首页  

c++11 STL 例子     所属分类 c 浏览量 781
std::array
跟数组类似,增加了迭代器功能

std::forward_list
c++11新增的线性表 单向链表 
list是双向链表

std::unordered_map与std::map
std::map 基于红黑树 有序
std::unordered_map  基于哈希表 无序

std::unordered_set  基于哈希表 无序
std::set 基于红黑树 有序


g++ -std=c++11 stldemo001.cpp #include <array> #include <forward_list> #include <unordered_map> #include <string> #include <unordered_set> #include <set> #include <iostream> using namespace std; int main(){ array<int, 3> arrayDemo = { 1,2,3}; for (auto item : arrayDemo){ cout << item << " "; } cout << endl; int arrayDemoSize = sizeof(arrayDemo); cout << "arrayDemo size:" << arrayDemoSize << endl; // forward_list<int> numbers = {1,2,3,2,2}; for (auto number : numbers){ cout << number << " "; } cout << endl ; numbers.remove(2); cout << "numbers after remove:" << endl; for (auto number : numbers){ cout << number << " "; } cout << endl ; // unordered_map<string, string> mymap ={ { "a","1" }, { "b","2" }, { "c","3" } }; int bucketCount = mymap.bucket_count(); cout << "mymap bucketCount " << bucketCount << endl; for (int i = 0; i<bucketCount; ++i) { cout << "bucket #" << i << " contains: "; for (auto it = mymap.begin(i); it != mymap.end(i); ++it){ cout << "[" << it->first << ":" << it->second << "] "; } cout << endl; } unordered_set<int> unorderSet; unorderSet.insert(7); unorderSet.insert(5); unorderSet.insert(3); unorderSet.insert(3); unorderSet.insert(6); std::cout << "unorderSet:" << std::endl; for (auto item : unorderSet){ std::cout << item << " "; } cout << endl; std::set<int> orderSet; orderSet.insert(7); orderSet.insert(5); orderSet.insert(3); orderSet.insert(3); orderSet.insert(6); std::cout << "orderSet:" << std::endl; for (auto item : orderSet){ std::cout << item << " "; } cout << endl; return 0; }
g++ -std=c++11 stldemo001.cpp ./a.out 1 2 3 arrayDemo size:12 1 2 3 2 2 numbers after remove: 1 3 mymap bucketCount 5 bucket #0 contains: bucket #1 contains: bucket #2 contains: [c:3] [a:1] bucket #3 contains: [b:2] bucket #4 contains: unorderSet: 6 3 5 7 orderSet: 3 5 6 7

上一篇     下一篇
C++动态内存分配实例

现代C++特性

C++防止头文件被重复引入的3种方法

c++模板和java泛型

c++11 线程实例

NULL 与 nullptr