//============================================================================ // Name : STL_1.cpp // Author : 齐保元 // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <iostream> #include <vector> #include <numeric> #include <algorithm> #include <string> #include <sstream> #include <set> #include <map> #include <queue> #include <bitset> #include <stdio.h> using namespace std; //是否运行vectorTest bool vectorTestRun = false; // 是否运行stringTest bool stringTestRun = false; //是否运行setTest bool setTestRun = false; //是否运行mapTest bool mapTestRun = false; //是否运行bitsetTest bool bitsetTestRun = true; //是否运行priorityTest bool priorityTestRun = true; /** * setCmp,自定义set排序函数。重载"()"操作符 */ struct setCmp { bool operator()(const int& a, const int& b) { return a < b; } }; bool Cmp(const int& a, const int& b) { return a > b; } /** * double转换为string */ string convertToString(double d) { ostringstream os; if (os << d) return os.str(); return "invalid conversion"; } /** * double转string */ double convertFromString(string str) { istringstream iss(str); double x; if (iss >> x) return x; return 0.0; } //vector测试 void vectorTest() { if (vectorTestRun == false) return; vector<int> v; int i; for (i = 0; i < 10; i++) { v.push_back(i); } cout << v.size() << endl; //reverse reverse(v.begin(), v.end()); //sort, 可以自定义比较函数 sort(v.begin(), v.end(), Cmp); for (vector<int>::iterator it = v.begin(); it != v.end(); ++it) { cout << *it << " "; } cout << endl; //统计所有元素的和 cout << accumulate(v.begin(), v.end(), 0) << endl; //清空 vector v.clear(); //判断是否为空 cout << v.empty() << endl; } //stringTest测试string void stringTest() { if (stringTestRun == false) return; string s = "hello,stl"; s += 'a'; s.append("_appended"); cout << s << ":" << s.length() << endl; string::iterator it; it = s.begin(); s.erase(it + 1, it + 4); s.insert(it + 1, 'p'); s.replace(3, 3, "fucku"); cout << s << ":" << s.length() << endl; reverse(s.begin(), s.end()); cout << s << ":" << s.length() << endl; cout << s.find("fuck") << endl; cout << s.compare("a") << endl; int x, y, z; sscanf("12 3 5", "%d %d %d", &x, &y, &z); cout << x << " " << y << " " << z << endl; string cc = convertToString(109.0); cout << cc << endl; double d = convertFromString("109.990"); cout << d << endl; } struct Info { string name; float score; // 重载“<” bool operator<(const Info &a) const { return a.score < score; } }; /** * setTest */ void setTest() { if (setTestRun == false) return; //实现了红黑树的平衡二叉树 set<int, setCmp> s; s.insert(8); s.insert(1); s.insert(12); s.insert(6); s.insert(8);// 去掉这个重复元素 //中序遍历 for (set<int>::iterator it = s.begin(); it != s.end(); ++it) { cout << *it << " "; } cout << endl; //反向中序 for (set<int>::reverse_iterator rit = s.rbegin(); rit != s.rend(); ++rit) { cout << *rit << " "; } cout << endl; set<int>::iterator it; it = s.find(6); if (it != s.end()) cout << *it << endl; set<Info> sinfo; } /** * 测试map */ void mapTest() { if (false == mapTestRun) return; map<int, char> m; m[1] = 'v'; m[4] = 'm'; for (map<int, char>::iterator it = m.begin(); it != m.end(); ++it) { cout << (*it).first << ":" << (*it).second << " "; } } /** * 测试bitset */ void bitsetTest() { if (false == bitsetTestRun) return; bitset<10> bit; bit.set(); bit.set(3, 0); cout << bit << endl; } /* * 测试priorityTest */ void priorityTest() { //总是弹出最大的元素 if (false == priorityTestRun) return; priority_queue<int> pq; pq.push(10); pq.push(90); pq.push(8); pq.push(80); while (pq.empty() != true) { cout << pq.top() << " "; pq.pop(); } } int main() { vectorTest(); stringTest(); setTest(); mapTest(); bitsetTest(); priorityTest(); return 0; }