#include <iostream> using namespace std; const int MAX=100; class Seqstack{ public: int Gettop(); void Push(int x); Seqstack(); private: int top; int data[MAX]; }; Seqstack::Seqstack(){ top=-1; } void Seqstack::Push(int x){ cout<<"enter a number:"<<endl; cin>>x; data[++top]=x; cout<<top<<endl; } int Seqstack::Gettop(){ int x; x=data[top]; cout<<x; return 0; } void main() { Seqstack s; s.Push(5); s.Push(4); s.Gettop(); }
#include <iostream> #include <stack> using namespace std; int main() { int s; int n; int k; cout<<"输入一个需要转化进制的整数:"<<endl; cin>>s; cout<<"输入你想要转化的进制(2-16):"<<endl; cin>>n; stack <int> stk; do { stk.push( s % n); s = s/n; } while (s!=0); while(!stk.empty()) { k = stk.top(); stk.pop(); cout<< k<<" "; } return 0; }
#include <iostream> using namespace std; const int Max=100;
class Seqlist{ public: Seqlist(int a[],int n); void Get(int i); private: int length; int b[Max]; }; Seqlist::Seqlist(int a[],int n) { for(int i=0;i<n;i++) { b[i]=a[i]; cout<<b[i]<<" "; } length=n; } void Seqlist::Get(int x){ for(int i=0;i<length;i++) { if(x==b[i]) { cout<<i; } else { } } } void main() { int n; int *a; int x; cout<<"please enter two number:"<<endl; cin>>n; cin>>x; a=new int[n]; cout<<"enter "<<n<<" numbers:"; for(int i=0;i<n;i++) cin>>a[i]; Seqlist s(a,n); s.Get(x); }
1.find 运算 find 运算是基于迭代器的,因此可在任意容器中使用相同的find函数查找值。 下面举些简单例子: #include <iostream> #include <algorithm> #include <list> #include <iterator> using namespace std; int main() { list <int> l; int t; while(cin>>t) l.push_front(t); int se; cout<<"输入你想要查找的数:"<<endl; cin.clear(); cin.sync(); cin>> se; list<int> :: const_iterator result = find (l.begin
(),l.end(),se); cout<<"the value "<<se<< (result == l.end()? " is not
present": "is present")<<endl; return 0; } 类似地,由于指针的行为与作用在内置数组的迭代器一样,因此可用find来搜索数组: #include <iostream> #include <algorithm> using namespace std; int main() { int a[] = {1,2,3,4,5}; int b = 7; int *p = find(a,a+5,b); cout<<"the value "<<b<<(p==a+5? "is not present":"is
present")<<endl; return 0; }
|
|
| 日 | 一 | 二 | 三 | 四 | 五 | 六 |
---|
27 | 28 | 29 | 30 | 31 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|
常用链接
留言簿(1)
随笔分类
随笔档案
搜索
最新评论
阅读排行榜
评论排行榜
|
|