一、容器
1.顺序容器
<list> :双向链表:begin\end\size\max_size\empty\front\back\emplace_front\push_front\pop_front\emplace_back Construct and insert element at the end \push_back\pop_back\emplace
<forward_list> :单向链表:
<set>: 关键字即值,即只保存关键字的容器
<unordered_set>: 用哈希函数组织的set
二、输入输出流
<iostream>: cin\cout\cerr\clog;
clog对应于标准错误输出流,用于向屏幕输出错误信息,不能被重定向。
cerr和clog的区别在于:cerr不适用缓冲区,直接向显示器输出信息;而输出到clog中的信息会先被存放到缓冲区,缓冲区满或者刷新时才输出到屏幕。
三、原子和线程(不常用,用到再补充)
intmyints[] = { 10, 20, 30, 40 };int* p;
p = std::find (myints, myints+4, 30);if(p != myints+4)
std::cout <<"Element found in myints: "<< *p <<'\n';elsestd::cout <<"Element not found in myints\n";// using std::find with vector and iterator:std::vector<int> myvector (myints,myints+4);
std::vector<int>::iterator it;
it = find (myvector.begin(), myvector.end(), 30);if(it != myvector.end()) boolIsOdd (inti) {return((i%2)==1);
}intmain () {
std::vector<int> myvector;
myvector.push_back(10);
myvector.push_back(25);
myvector.push_back(40);
myvector.push_back(55);
std::vector<int>::iterator it = std::find_if (myvector.begin(), myvector.end(), IsOdd);
std::cout <<"The first odd value is "<< *it <<'\n';return0;
} intmyints[] = {10,20,30,30,20,10,10,20};// 8 elementsintmycount = std::count (myints, myints+8, 10);
std::cout <<"10 appears "<< mycount <<" times.\n";// counting elements in container:std::vector<int> myvector (myints, myints+8);
mycount = std::count (myvector.begin(), myvector.end(), 20);
std::cout <<"20 appears "<< mycount <<" times.\n"; intmyints[] = {20,40,60,80,100};// myints: 20 40 60 80 100std::vector<int>myvector (myints,myints+5);// myvector: 20 40 60 80 100// using default comparison:if( std::equal (myvector.begin(), myvector.end(), myints) )
std::cout <<"The contents of both sequences are equal.\n";elsestd::cout <<"The contents of both sequences differ.\n";
myvector[3]=81;// myvector: 20 40 60 81 100// using predicate comparison:if( std::equal (myvector.begin(), myvector.end(), myints, mypredicate) )
std::cout <<"The contents of both sequences are equal.\n";elsestd::cout <<"The contents of both sequences differ.\n"; boolmypredicate (inti,intj) {return(i==j);
}intmain () {
std::vector<int> haystack;// set some values: haystack: 10 20 30 40 50 60 70 80 90for(inti=1; i<10; i++) haystack.push_back(i*10);// using default comparison:intneedle1[] = {40,50,60,70};
std::vector<int>::iterator it;
it = std::search (haystack.begin(), haystack.end(), needle1, needle1+4);if(it!=haystack.end())
std::cout <<"needle1 found at position "<< (it-haystack.begin()) <<'\n';elsestd::cout <<"needle1 not found\n";// using predicate comparison:intneedle2[] = {20,30,50};
it = std::search (haystack.begin(), haystack.end(), needle2, needle2+3, mypredicate);if(it!=haystack.end())
std::cout <<"needle2 found at position "<< (it-haystack.begin()) <<'\n';elsestd::cout <<"needle2 not found\n";return0;
} bool myfunction (inti,intj) {return(i<j); }
struct myclass {booloperator() (inti,intj) {return(i<j);}
} myobject;
int main () {intmyints[] = {32,71,12,45,26,80,53,33};
std::vector<int> myvector (myints, myints+8);
std::sort (myvector.begin(), myvector.begin()+4);
std::sort (myvector.begin()+4, myvector.end(), myfunction);
std::sort (myvector.begin(), myvector.end(), myobject); std::cout <<"min(1,2)=="<< std::min(1,2) <<'\n';
std::cout <<"min(2,1)=="<< std::min(2,1) <<'\n';
std::cout <<"min('a','z')=="<< std::min('a','z') <<'\n';
std::cout <<"min(3.14,2.72)=="<< std::min(3.14,2.72) <<'\n'; #include <iostream> #include <vector>
int main () {intfoo[] = {10,20,30,40,50};
std::vector<int> bar;
for(autoit = std::begin(foo); it!=std::end(foo); ++it)
bar.push_back(*it);
std::cout <<"bar contains:";
for(autoit = std::begin(bar); it!=std::end(bar); ++it) std::cout <<' '<< *it;
std::cout <<'\n';
return 0; #include <string> #include <iostream> #include <iterator> #include <map> using namespace std; int main() {map<int, string> map1 = {{ 7,"a1" }, { 8, "a2" }, {9,"a3" }}; for (auto it = map1.begin(); it != map1.end(); it++){ if (prev(it) == map1.begin()) cout << "第二个元素 " << map1[it->first] << endl; // a2 if (next(it) == map1.end()) cout << "最后一个元素 " << map1[it->first] << endl; // a3 } }
structMyClass {
intdata[100];
MyClass() {std::cout <<"constructed ["<<this<<"]\n";}
};
int main () {
std::cout <<"1: ";
MyClass * p1 =newMyClass;
std::cout <<"2: ";
MyClass * p2 =new(std::nothrow) MyClass;
std::cout <<"3: ";
new(p2) MyClass;
std::cout <<"4: ";
MyClass * p3 = (MyClass*) ::operatornew(sizeof(MyClass));
deletep1;deletep2;deletep3;
return0;
} 
京公网安备 11010502036488号