int num; stringstream stream; stream<< num; string s; s=stream.str(); //此处也可以用 stream>>s
sort(map.begin(), map.end(), compare); bool compare(int a, int b) { return a < b; }当函数简短时,也可以用c++11的lambda表达式写成下面的形式:
sort(map.begin(), map.end(), [](int a, int b) { return a<b; });
输出vector最小值:
vector<int> vec(arry,arry+size); sort(vec.begin(),vec.end()); cout << "there is " << count(vec.begin(),vec.end(),*vec.begin()); cout << " min numbers,"<< "value:" << *vec.begin() << endl;
指针new and delete
int *arry = NULL; int size=4; arry = new int[size]; int i = 0; while (i != size) { cin >> arry[i++]; } delete []arry;
通过迭代器遍历数组元素:
for (vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); ++iter) *iter = 0;
使用下标word_count["foobar"] 有危险的副作用:如果该键不在map容器中,那么会插入一个具有该键的新元素。
m.count(k) | 返回m中k的出现次数 |
m.find(k) | 若m容器中存在按k索引的元素,返回指向该元素的迭代器; 若不存在,则返回超出末端迭代器 |
int occurs = 0; if (word_count.count("foobar")) //count returns 0 or 1 occurs = word_count["foobar"];
int occurs = 0; map<string, int>::iterator it = word_count.find("foobar"); if (it != word_count.end()) occurs = it->second;
Iterator Category Ability Providers Input iterator Reads forward istream Output iterator Writes forward tream, inserter Forward iterator Reads and writes forward Bidirectional iterator Reads and writes forward and backward list, set, multiset, map, multimap Random access iterator Reads and writes with random access vector, deque string, array
详情可见 : http://stdcxx.apache.org/doc/stdlibug/2-2.html