作者在 2008-08-17 14:12:46 发布以下内容
#include<iostream>
#include<vector>
#include<algorithm>
#include<iterator>
using namespace std;
void Pause()
{
char c;
cout<<"\npress return to continue: ";
cin.get(c);
cout<<endl;
}
int main()
{
vector<int> v(10,0);
ostream_iterator<int> out(cout," "); //定义一个输出迭代器
copy(v.begin(),v.end(),out);
vector<int>::iterator i = v.begin(); //定义头迭代器
i += 4;
*i++ = 7; //or v[4] = 7,使第五个元素值为7,同时迭代器指向下一元素
*i = 9; //or v[5] = 9
copy(v.begin(),v.end(),out);
Pause();
vector<int>::iterator where = find(v.begin(),v.end(),9); //在V中找元素9并返回相应迭代器
//copy(where,v.end(),out); //输出where 以后元素
Pause();
where += 3;
where = v.insert(where,4); //在当前元素前插入一个元素,值为4
copy(v.begin(),v.end(),out);
Pause();
where -= 6;
where = v.insert(where,11);
copy(v.begin(),v.end(),out);
Pause();
v.erase(where+2); //删除迭代器后的第二个元素
copy(v.begin(),v.end(),out);
sort(v.begin(),v.end()); //对VECTOR进行由大到小排序
copy(v.begin(),v.end(),out);
Pause();
if (binary_search(v.begin(),v.end(),8)) //vector的查找
cout<<"find!"<<endl;
else
cout<<"can't find!"<<endl;
where = lower_bound(v.begin(),v.end(),8); //查找第一次出现8的地方
copy(where,v.end(),out);
Pause();
where = upper_bound(v.begin(),v.end(),0); //查找第一次不出现 0 的位置
copy(where,v.end(),out);
Pause();
///////////////////////////////////////
vector<int> w(v);
w[5] = 18;
if (w==v) cout<<"some!"<<endl;
else cout<<"not some!"<<endl;
v[5] = 18;
if (w==v) cout<<"some!"<<endl;
else cout<<"not some!"<<endl;
Pause();
return 0;
}
#include<vector>
#include<algorithm>
#include<iterator>
using namespace std;
void Pause()
{
char c;
cout<<"\npress return to continue: ";
cin.get(c);
cout<<endl;
}
int main()
{
vector<int> v(10,0);
ostream_iterator<int> out(cout," "); //定义一个输出迭代器
copy(v.begin(),v.end(),out);
vector<int>::iterator i = v.begin(); //定义头迭代器
i += 4;
*i++ = 7; //or v[4] = 7,使第五个元素值为7,同时迭代器指向下一元素
*i = 9; //or v[5] = 9
copy(v.begin(),v.end(),out);
Pause();
vector<int>::iterator where = find(v.begin(),v.end(),9); //在V中找元素9并返回相应迭代器
//copy(where,v.end(),out); //输出where 以后元素
Pause();
where += 3;
where = v.insert(where,4); //在当前元素前插入一个元素,值为4
copy(v.begin(),v.end(),out);
Pause();
where -= 6;
where = v.insert(where,11);
copy(v.begin(),v.end(),out);
Pause();
v.erase(where+2); //删除迭代器后的第二个元素
copy(v.begin(),v.end(),out);
sort(v.begin(),v.end()); //对VECTOR进行由大到小排序
copy(v.begin(),v.end(),out);
Pause();
if (binary_search(v.begin(),v.end(),8)) //vector的查找
cout<<"find!"<<endl;
else
cout<<"can't find!"<<endl;
where = lower_bound(v.begin(),v.end(),8); //查找第一次出现8的地方
copy(where,v.end(),out);
Pause();
where = upper_bound(v.begin(),v.end(),0); //查找第一次不出现 0 的位置
copy(where,v.end(),out);
Pause();
///////////////////////////////////////
vector<int> w(v);
w[5] = 18;
if (w==v) cout<<"some!"<<endl;
else cout<<"not some!"<<endl;
v[5] = 18;
if (w==v) cout<<"some!"<<endl;
else cout<<"not some!"<<endl;
Pause();
return 0;
}