标签:pac begin mes iter for 赋值 iostream it! iterator
#include<iostream> #include<vector> #include<numeric> using namespace std; int main(){ vector<int> v; int i; for(int i = 0;i < 10;i++){ //尾部元素扩张方式赋值 v.push_back(i); } //使用iterator迭代器顺序遍历所有元素 for(vector<int>::iterator it = v.begin();it!=v.end();it++){ cout << *it << " "; } cout << endl; cout << accumulate(v.begin(),v.end(),0) << endl; return 0; }
#include<iostream> #include<vector> #include<numeric> using namespace std; int main(){ vector<int> v(3); v[0] = 1; v[1] = 2; v[2] = 3; cout << v[0] << " " << v[1]<< endl; }
#include<iostream> #include<vector> #include<numeric> using namespace std; int main(){ vector<int> v(3); v[0] = 1; v[1] = 2; v[2] = 3; vector<int>::iterator it; for(it = v.begin();it != v.end();it++){ //输出迭代器上的元素值 cout << *it << " "; } return 0; }
#include<iostream> #include<vector> #include<numeric> using namespace std; int main(){ vector<int> v(3); v[0] = 1; v[1] = 2; v[2] = 3; //在前面插入新元素,元素值为8; v.insert(v.begin(),8); //在第二个元素前插入新元素1 v.insert(v.begin()+2,1); //在向量尾追加新元素3 v.insert(v.end(),3); //定义迭代器变量 vector<int>::iterator it; for(it = v.begin();it != v.end();it ++) { //输出迭代器的元素值 cout << *it << " "; } //换行 cout << endl; return 0; } //8 1 1 2 3 3
#include<iostream> #include<vector> #include<numeric> using namespace std; int main(){ vector<int> v(3); v[0] = 1; v[1] = 2; v[2] = 3; //在前面插入新元素,元素值为8; v.insert(v.begin(),8); //在第二个元素前插入新元素1 v.insert(v.begin()+2,1); //在向量尾追加新元素3 v.insert(v.end(),3); //定义迭代器变量 vector<int>::iterator it; for(it = v.begin();it != v.end();it ++) { //输出迭代器的元素值 cout << *it << " "; } //换行 cout << endl; return 0; }
标签:pac begin mes iter for 赋值 iostream it! iterator
原文地址:https://www.cnblogs.com/luyuan-chen/p/11920654.html