一、迭代器:把全部與迭代有關的東西全都抽象出來,好比數組的下標、指針、for裏面的、list裏面的、vector裏面的,抽象一下變成了iterator
#include <iostream>
#include <vector>ios
using namespace std;c++
int main()
{數組
vector<int> v; for(int i = 0; i < 10; ++i ) { v.push_back(i); } for(vector<int>::iterator it = v.begin(); it != v.end(); ++it) { cout << *it << " "; } cout << endl; return 0;
}
函數
二、求和(<numeric> accumulate)spa
accumulate(v.begin(),v.end(),0),把從 v.begin() 開始到 v.end()結束全部的元素加到 0上面去3d
using namespace std;指針
int main()
{code
vector<int> v; for (int i = 0; i < 10; ++i) { v.push_back(i); } for (vector<int>::iterator it = v.begin(); it != v.end(); ++it) { cout << *it << " "; } cout << endl; cout << accumulate(v.begin(), v.end(), 0) << endl; system("pause"); return 0;
}blog
三、vector(動態數組)排序
vector有內存管理的機制,也就是說對於插入和刪除,vector能夠動態調整所佔用的內存空間。
vector相關函數:begin()/end()/insert()/erase()
using namespace std;
int main()
{
vector<int> v; v.push_back(3); //數組尾部插入3 v.push_back(2); v.push_back(1); v.push_back(0); cout << " 下標 " << v[3] << endl; cout << " 迭代器 " << endl; for (vector<int>::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<int>::iterator i = v.begin(); i != v.end(); ++i) { cout << *i << " "; } cout << endl; vector<int> arr(10); for (int i = 0; i < 10; i++) { arr[i] = i; } for (vector<int>::iterator i = arr.begin(); i != arr.end(); ++i) { cout << *i << " "; } cout << endl; //刪除 同insert arr.erase(arr.begin()); for (vector<int>::iterator i = arr.begin(); i != arr.end(); ++i) { cout << *i << " "; } cout << endl; arr.erase(arr.begin(), arr.begin() + 5); for (vector<int>::iterator i = arr.begin(); i != arr.end(); ++i) { cout << *i << " "; } cout << endl; system("pause"); return 0;
}
四、數組轉置 (<algorithm> reverse)
reverse(v.begin(),v.end())
using namespace std;
int main()
{
vector<int> v; for(int i = 0; i < 10; ++i) { v.push_back(i); } for(vector<int>::iterator it = v.begin(); it != v.end(); ++it) { cout << *it << " "; } cout << endl; reverse(v.begin(),v.end()); for(vector<int>::iterator it = v.begin(); it != v.end(); ++it) { cout << *it << " "; } cout << endl; system("pause"); return 0; }
五、排序(<algorithm> sort)
sort(v.begin(),v.end())
using namespace std;
bool Comp(const int &a,const int &b)
{
return a>b;
}
int main()
{
vector<int> 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<int>::iterator it = v.begin(); it != v.end(); ++it) { cout << *it << " "; } cout << endl; //默認升序 sort(v.begin(),v.end()); for(vector<int>::iterator it = v.begin(); it != v.end(); ++it) { cout << *it << " "; } cout << endl; //用降序 須要自定義一個降序函數 sort(v.begin(),v.end(),Comp); for(vector<int>::iterator it = v.begin(); it != v.end(); ++it) { cout << *it << " "; } cout << endl; system("pause"); return 0;
}
六、刪除
s.erase(it + 1,it + 4); clear() #include<iostream>
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;
}
七、查找(find)
用find找到string裏面第一個要找到元素(char或者串),找到返回數組下標,找不到返回end()迭代器。string和vector有不少相同的東西,好比length(),size(),empty(),reverse(),相對也容易,就不一一說了。
八、數字化處理(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;
}
九、string與數值相互轉換( sprintf <sstream> )
using namespace std;
//c++ 方法 把數轉換爲string
string 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 error return 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; }
十、正反遍歷,迭代器iterator、reverse_iterator
using namespace std;
int main()
{
set<int> v; v.insert(1); v.insert(3); v.insert(5); v.insert(2); v.insert(4); v.insert(3); //中序遍歷 升序遍歷 for(set<int>::iterator it = v.begin(); it != v.end(); ++it) { cout << *it << " "; } cout << endl; for(set<int>::reverse_iterator rit = v.rbegin(); rit != v.rend(); ++rit) { cout << *rit << " "; } cout << endl; return 0;
}