C++之STL迭代器(iterator)
阿新 • • 發佈:2018-12-30
1、vector #include <iostream> #include <vector> int main() { std::vector<char> charVector; int x; for (x=0; x<10; ++x) charVector.push_back(65 + x); int size = charVector.size(); for (x=0; x<size; ++x) { std::vector<char>::iterator start = charVector.begin(); charVector.erase(start); std::vector<char>::iterator iter; for (iter = charVector.begin(); iter != charVector.end(); iter++) { std::cout << *iter; } std::cout << std::endl; } return 0; } 2、deque #include <iostream> #include <deque> int main() { std::deque<char> charDeque; int x; for (x=0; x<10; ++x) charDeque.push_front(65 + x); int size = charDeque.size(); for (x=0; x<size; ++x) { std::deque<char>::iterator start = charDeque.begin(); charDeque.erase(start); std::deque<char>::iterator iter; for (iter = charDeque.begin(); iter != charDeque.end(); iter++) { std::cout << *iter; } std::cout << std::endl; } return 0; } 3、list #include <iostream> #include <list> int main() { // Create and populate the list. int x; std::list<char> charList; for (x=0; x<10; ++x) charList.push_front(65 + x); // Display contents of list. std::cout << "Original list: "; std::list<char>::iterator iter; for (iter = charList.begin(); iter != charList.end(); iter++) { std::cout << *iter; //char ch = *iter; //std::cout << ch; } std::cout << std::endl; // Insert five Xs into the list. std::list<char>::iterator start = charList.begin(); charList.insert(++start, 5, 'X'); // Display the result. std::cout << "Resultant list: "; for (iter = charList.begin(); iter != charList.end(); iter++) { std::cout << *iter; //char ch = *iter; //std::cout << ch; } return 0; } 4、set #include <iostream> #include <set> int main() { // Create the set object. std::set<char> charSet; // Populate the set with values. charSet.insert('E'); charSet.insert('D'); charSet.insert('C'); charSet.insert('B'); charSet.insert('A'); // Display the contents of the set. std::cout << "Contents of set: " << std::endl; std::set<char>::iterator iter; for (iter = charSet.begin(); iter != charSet.end(); iter++) std::cout << *iter << std::endl; std::cout << std::endl; // Find the D. iter = charSet.find('D'); if (iter == charSet.end()) std::cout << "Element not found."; else std::cout << "Element found: " << *iter; return 0; } 5、multiset #include <iostream> #include <set> int main() { // Create the first set object. std::multiset<char> charMultiset1; // Populate the multiset with values. charMultiset1.insert('E'); charMultiset1.insert('D'); charMultiset1.insert('C'); charMultiset1.insert('B'); charMultiset1.insert('A'); charMultiset1.insert('B'); charMultiset1.insert('D'); // Display the contents of the first multiset. std::cout << "Contents of first multiset: " << std::endl; std::multiset<char>::iterator iter; for (iter = charMultiset1.begin(); iter != charMultiset1.end(); iter++) std::cout << *iter << std::endl; std::cout << std::endl; // Create the second multiset object. std::multiset<char> charMultiset2; // Populate the multiset with values. charMultiset2.insert('J'); charMultiset2.insert('I'); charMultiset2.insert('H'); charMultiset2.insert('G'); charMultiset2.insert('F'); charMultiset2.insert('G'); charMultiset2.insert('I'); // Display the contents of the second multiset. std::cout << "Contents of second multiset: " << std::endl; for (iter = charMultiset2.begin(); iter != charMultiset2.end(); iter++) std::cout << *iter << std::endl; std::cout << std::endl; // Compare the sets. if (charMultiset1 == charMultiset2) std::cout << "set1 == set2"; else if (charMultiset1 < charMultiset2) std::cout << "set1 < set2"; else if (charMultiset1 > charMultiset2) std::cout << "set1 > set2"; return 0; } 6、map #include <iostream> #include <map> typedef std::map<int, char> MYMAP; int main() { // Create the first map object. MYMAP charMap1; // Populate the first map with values. charMap1[1] = 'A'; charMap1[4] = 'D'; charMap1[2] = 'B'; charMap1[5] = 'E'; charMap1[3] = 'C'; // Display the contents of the first map. std::cout << "Contents of first map: " << std::endl; MYMAP::iterator iter; for (iter = charMap1.begin(); iter != charMap1.end(); iter++) { std::cout << (*iter).first << " --> "; std::cout << (*iter).second << std::endl; } std::cout << std::endl; // Create the second map object. MYMAP charMap2; // Populate the first map with values. charMap2[1] = 'F'; charMap2[4] = 'I'; charMap2[2] = 'G'; charMap2[5] = 'J'; charMap2[3] = 'H'; // Display the contents of the second map. std::cout << "Contents of second map: " << std::endl; for (iter = charMap2.begin(); iter != charMap2.end(); iter++) { std::cout << (*iter).first << " --> "; std::cout << (*iter).second << std::endl; } std::cout << std::endl; // Compare the maps. if (charMap1 == charMap2) std::cout << "map1 == map2"; else if (charMap1 < charMap2) std::cout << "map1 < map2"; else if (charMap1 > charMap2) std::cout << "map1 > map2"; return 0; } 7、multimap #include <iostream> #include <map> typedef std::multimap<int, char> MYMAP; int main() { // Create the first multimap object. MYMAP charMultimap; // Populate the multimap with values. charMultimap.insert(MYMAP::value_type(1,'A')); charMultimap.insert(MYMAP::value_type(4,'C')); charMultimap.insert(MYMAP::value_type(2,'B')); charMultimap.insert(MYMAP::value_type(7,'E')); charMultimap.insert(MYMAP::value_type(5,'D')); charMultimap.insert(MYMAP::value_type(3,'B')); charMultimap.insert(MYMAP::value_type(6,'D')); // Display the contents of the first multimap. std::cout << "Contents of first multimap: " << std::endl; MYMAP::iterator iter; for (iter = charMultimap.begin(); iter != charMultimap.end(); iter++) { std::cout << (*iter).first << " --> "; std::cout << (*iter).second << std::endl; } std::cout << std::endl; // Create the second multimap object. MYMAP charMultimap2; // Populate the second multimap with values. charMultimap2.insert(MYMAP::value_type(1,'C')); charMultimap2.insert(MYMAP::value_type(4,'F')); charMultimap2.insert(MYMAP::value_type(2,'D')); charMultimap2.insert(MYMAP::value_type(7,'E')); charMultimap2.insert(MYMAP::value_type(5,'F')); charMultimap2.insert(MYMAP::value_type(3,'E')); charMultimap2.insert(MYMAP::value_type(6,'G')); // Display the contents of the second multimap. std::cout << "Contents of second multimap: " << std::endl; for (iter = charMultimap2.begin(); iter != charMultimap2.end(); iter++) { std::cout << (*iter).first << " --> "; std::cout << (*iter).second << std::endl; } std::cout << std::endl; // Compare the multimaps. if (charMultimap == charMultimap2) std::cout << "multimap1 == multimap2"; else if (charMultimap < charMultimap2) std::cout << "multimap1 < multimap2"; else if (charMultimap > charMultimap2) std::cout << "multimap1 > multimap2"; return 0; } 8、stack #include <iostream> #include <list> #include <stack> int main() { std::stack<int, std::list<int> > intStack; int x; std::cout << "Values pushed onto stack:" << std::endl; for (x=1; x<11; ++x) { intStack.push(x*100); std::cout << x*100 << std::endl; } std::cout << "Values popped from stack:" << std::endl; int size = intStack.size(); for (x=0; x<size; ++x) { std::cout << intStack.top() << std::endl; intStack.pop(); } return 0; } 9、queue #include <iostream> #include <list> #include <queue> int main() { std::queue<int, std::list<int> > intQueue; int x; std::cout << "Values pushed onto queue:" << std::endl; for (x=1; x<11; ++x) { intQueue.push(x*100); std::cout << x*100 << std::endl; } std::cout << "Values removed from queue:" << std::endl; int size = intQueue.size(); for (x=0; x<size; ++x) { std::cout << intQueue.front() << std::endl; intQueue.pop(); } return 0; } 10、priority_queue #include <iostream> #include <list> #include <queue> int main() { std::priority_queue<int, std::vector<int>,std::greater<int> > intPQueue; int x; intPQueue.push(400); intPQueue.push(100); intPQueue.push(500); intPQueue.push(300); intPQueue.push(200); std::cout << "Values removed from priority queue:" << std::endl; int size = intPQueue.size(); for (x=0; x<size; ++x) { std::cout << intPQueue.top() << std::endl; intPQueue.pop(); } return 0; }