1、迭代器:把所有与迭代有关的东西全都抽象出来,比如数组的下标、指针、for里面的、list里面的、vector里面的,抽象一下变成了iterator
#include <iostream> #include <vector>using namespace std;
int main()
{vector v; for(int i = 0; i < 10; ++i ) { v.push_back(i); } for(vector ::iterator it = v.begin(); it != v.end(); ++it) { cout << *it << " "; } cout << endl; return 0;
}
2、求和(<numeric> accumulate)
accumulate(v.begin(),v.end(),0),把从 v.begin() 开始到 v.end()结束所有的元素加到 0上面去
include <iostream>
include <vector>
include <numeric>
using namespace std;
int main()
{vector v;for (int i = 0; i < 10; ++i){ v.push_back(i);}for (vector ::iterator it = v.begin(); it != v.end(); ++it){ cout << *it << " ";}cout << endl;cout << accumulate(v.begin(), v.end(), 0) << endl;system("pause");return 0;
}
3、vector(动态数组)
vector有内存管理的机制,也就是说对于插入和删除,vector可以动态调整所占用的内存空间。
vector相关函数:begin()/end()/insert()/erase()
include <iostream>
include <vector>
using namespace std;
int main()
{vector v;v.push_back(3); //数组尾部插入3v.push_back(2);v.push_back(1);v.push_back(0);cout << " 下标 " << v[3] << endl;cout << " 迭代器 " << endl;for (vector ::iterator i = v.begin(); i != v.end(); ++i){ cout << *i << " ";}cout << endl;//在第一个元素之前插入111 insert begin+n是在第n个元素之前插入v.insert(v.begin(), 111);//在最后一个元素之后插入222 insert end + n 是在n个元素之后插入v.insert(v.end(), 222);//v.insert(v.end()-1, 666);for (vector ::iterator i = v.begin(); i != v.end(); ++i){ cout << *i << " ";}cout << endl;vector arr(10);for (int i = 0; i < 10; i++){ arr[i] = i;}for (vector ::iterator i = arr.begin(); i != arr.end(); ++i){ cout << *i << " ";}cout << endl;//删除 同insertarr.erase(arr.begin());for (vector ::iterator i = arr.begin(); i != arr.end(); ++i){ cout << *i << " ";}cout << endl;arr.erase(arr.begin(), arr.begin() + 5);for (vector ::iterator i = arr.begin(); i != arr.end(); ++i){ cout << *i << " ";}cout << endl;system("pause");return 0;
}
4、数组转置 (<algorithm> reverse)
reverse(v.begin(),v.end())
include<iostream>
include<vector>
include<algorithm>
using namespace std;
int main()
{vector v;for(int i = 0; i < 10; ++i){ v.push_back(i);}for(vector ::iterator it = v.begin(); it != v.end(); ++it){ cout << *it << " ";}cout << endl;reverse(v.begin(),v.end());for(vector ::iterator it = v.begin(); it != v.end(); ++it){ cout << *it << " ";}cout << endl;system("pause");return 0;}
5、排序(<algorithm> sort)
sort(v.begin(),v.end())include<iostream>
include<vector>
include<algorithm>
using namespace std;
bool Comp(const int &a,const int &b)
{return a>b;
}
int main()
{vector v;v.push_back(1);v.push_back(3);v.push_back(2);v.push_back(55);v.push_back(-1);v.push_back(0);v.push_back(2);v.push_back(3);v.push_back(4);for(vector ::iterator it = v.begin(); it != v.end(); ++it){ cout << *it << " ";}cout << endl;//默认升序sort(v.begin(),v.end());for(vector ::iterator it = v.begin(); it != v.end(); ++it){ cout << *it << " ";}cout << endl;//用降序 需要自定义一个降序函数sort(v.begin(),v.end(),Comp);for(vector ::iterator it = v.begin(); it != v.end(); ++it){ cout << *it << " ";}cout << endl;system("pause");return 0;
}
6、删除
s.erase(it + 1,it + 4); clear()#include
include<string>
using namespace std;
int main()
{string s;s = "0123456789";cout << s << endl;string::iterator it = s.begin();//删除s[3]s.erase(it+3);cout << s << endl;//删除s[1]~s[3]s = "0123456789";s.erase(it + 1,it + 4);cout << s << endl;//全部删除s.clear();cout << "clear : " << s << endl;return 0;
}
7、查找(find)
用find找到string里面第一个要找到元素(char或者串),找到返回数组下标,找不到返回end()迭代器。string和vector有很多相同的东西,比如length(),size(),empty(),reverse(),相对也容易,就不一一说了。
8、数字化处理(string)
经常会遇到这样一种情况,有一个数字,需要把每一位给提取出来,如果用取余数的方法,花费的时间就会很长,所以可以当成字符串来处理,方便、省时。
例子:求一个整数各位数的和
include<iostream>
include<string>
using namespace std;
int main()
{string s;s = "123456789";int sum = 0;for(int i = 0; i < s.size(); ++i){ switch(s[i]) { case '1': sum += 1;break; case '2': sum += 2;break; case '3': sum += 3;break; case '4': sum += 4;break; case '5': sum += 5;break; case '6': sum += 6;break; case '7': sum += 7;break; case '8': sum += 8;break; case '9': sum += 9;break; }}cout << sum << endl;return 0;
}
9、string与数值相互转换( sprintf <sstream> )
include<iostream>
include<string>
include<sstream>
include<cstdio>
using namespace std;
//c++ 方法 把数转换为stringstring converToString(double x){ostringstream o;if( o << x){ // str()没有'\0' c_str有 return o.str();}return "error";}double converFromString(const string &s){istringstream i(s);double x;if( i >> x){ return x;}//if errorreturn 0.0;}int main(){ char b[100]; string s1; //c语言方法 sprintf(b,"%d",1987); s1 = b; cout << s1 << endl; string s2 = converToString(1954); cout << s2 << endl; string s3 = "202"; int c = converFromString(s3); cout << c << endl; string s4 = "casacsa6"; int d = converFromString(s4); cout << d << endl; string s5 = "21abf4"; int f = converFromString(s5); cout << f << endl; return 0;}
10、正反遍历,迭代器iterator、reverse_iterator
include<iostream>
include<set>
using namespace std;
int main()
{set v;v.insert(1);v.insert(3);v.insert(5);v.insert(2);v.insert(4);v.insert(3);//中序遍历 升序遍历for(set ::iterator it = v.begin(); it != v.end(); ++it){ cout << *it << " ";}cout << endl;for(set ::reverse_iterator rit = v.rbegin(); rit != v.rend(); ++rit){ cout << *rit << " ";}cout << endl;return 0;
}