容器(圖片摘錄至資料來源1的P.104)
檔名:vector1.cpp檔案內容:
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> col1;
int i;
for (i=1;i<=6;++i) {
col1.push_back(i);
}
cout << col1.size() <<endl;
for(i=0;i<col1.size();++i) {
cout << col1[i] <<' ';
}
cout << endl;
}
檔名:deque1.cpp
檔案內容:
#include <iostream>
#include <deque>
using namespace std;
int main() {
deque<int> col1;
int i;
for (i=1;i<=6;i++) {
col1.push_front(i);
}
for (i=0;i<col1.size();i++) {
cout << col1[i] <<' ';
}
cout << endl;
}
檔名:list1.cpp
檔案內容:
#include <iostream>
#include <list>
using namespace std;
int main() {
list<char> coll;
for (char c='a';c<='z';c++) {
coll.push_back(c);
}
while(!coll.empty()) {
cout << coll.front() <<' ';
coll.pop_front();
}
cout << endl;
for (char c='a';c<='z';c++) {
coll.push_back(c);
}
while(!coll.empty()) {
cout << coll.back() <<' ';
coll.pop_back();
}
cout << endl;
}
檔名:list2.cpp
檔案內容:
#include <iostream>
#include <list>
using namespace std;
int main() {
list<char> coll;
list<char>::const_iterator pos;
for (char c='a';c<='z';c++) {
coll.push_back(c);
}
for (pos=coll.begin();pos!=coll.end();pos++) {
cout << *pos <<' ';
}
cout<<endl;
}
檔名:set1.cpp
檔案內容:
#include <iostream>
#include <set>
using namespace std;
int main() {
set<int> coll;
set<int>::const_iterator pos;
for (int a=1;a<10;++a) {
coll.insert(a);
}
coll.insert(1);
coll.insert(3);
coll.insert(5);
for (pos=coll.begin();pos!=coll.end();++pos) {
cout << *pos <<' ';
}
cout << endl;
}
檔名:mmap1.cpp
檔案內容:
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main() {
typedef multimap<int,string> IntStringMMap;
IntStringMMap coll;
coll.insert(make_pair(5,"tagged"));
coll.insert(make_pair(2,"a"));
coll.insert(make_pair(1,"this"));
coll.insert(make_pair(4,"of"));
coll.insert(make_pair(6,"strings"));
coll.insert(make_pair(1,"is"));
coll.insert(make_pair(3,"multimap"));
IntStringMMap::iterator pos;
for (pos=coll.begin();pos!=coll.end();++pos) {
cout << pos->second <<' ';
}
cout << endl;
for (pos=coll.begin();pos!=coll.end();++pos) {
cout << (*pos).second <<' ';
}
cout << endl;
for (pos=coll.begin();pos!=coll.end();++pos) {
cout << pos->first <<' ';
}
cout << endl;
for (pos=coll.begin();pos!=coll.end();++pos) {
cout << (*pos).first <<' ';
}
cout << endl;
}
檔名:map1.cpp
檔案內容:
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main() {
typedef map<string,float> StringFloatMap;
StringFloatMap coll;
coll["VAT"]=0.15;
coll["Pi"]=3.1415;
coll["an arbitrary number"] = 4983.223;
coll["Null"]=0;
StringFloatMap::iterator pos;
for (pos=coll.begin();pos!=coll.end();++pos) {
cout << "key: \"" << pos->first << "\" "
<< "value: " << pos->second << endl;
}
}
檔名:algo1.cpp
檔案內容:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> coll;
vector<int>::iterator pos;
coll.push_back(2);
coll.push_back(5);
coll.push_back(4);
coll.push_back(1);
coll.push_back(6);
coll.push_back(3);
for (pos=coll.begin();pos!=coll.end();++pos) {
cout << *pos << ' ';
}
cout << endl;
pos = min_element(coll.begin(),coll.end());
cout << "min: " << *pos << endl;
cout << "min: " << *min_element(coll.begin(),coll.end()) << endl;
pos = max_element(coll.begin(),coll.end());
cout << "max: " << *pos << endl;
cout << "max: " << *max_element(coll.begin(),coll.end()) << endl;
sort(coll.begin(),coll.end());
for (pos=coll.begin();pos!=coll.end();++pos) {
cout << *pos << ' ';
}
cout << endl;
pos = find(coll.begin(),coll.end(),3);
cout << *pos << endl;
reverse(pos,coll.end());
for (pos=coll.begin();pos!=coll.end();++pos) {
cout << *pos << ' ';
}
cout << endl;
}
檔名:find1.cpp
檔案內容:
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
int main() {
list<int> coll;
list<int>::iterator pos;
for (int i=20;i<=40;++i) {
coll.push_back(i);
}
pos = find(coll.begin(),coll.end(),3);
cout << "pos = "<<*pos <<endl;
reverse(pos,coll.end());
for (pos = coll.begin();pos!=coll.end();++pos) {
cout << *pos <<' ';
}
cout << endl;
list<int>::iterator pos25,pos35,pos30;
pos25 = find(coll.begin(),coll.end(),25);
cout << "pos25: " << *pos25 << endl;
pos35 = find(coll.begin(),coll.end(),35);
cout << "pos35: " << *pos35 << endl;
cout << "max: " << *max_element(pos25,pos35) << endl;
cout << "max: " << *max_element(pos25,++pos35) << endl;
++pos35;
pos30 = find(pos25,pos35,30);
cout << "pos30 = " << *pos30 << endl;
}
檔名:copy2.cpp
檔案內容:
#include <iostream>
#include <vector>
#include <list>
#include <deque>
#include <algorithm>
using namespace std;
int main() {
list<int> coll1;
list<int>::iterator pos1;
vector<int> coll2;
vector<int>::iterator pos2;
for (int i=1;i<=9;++i) {
coll1.push_back(i);
}
cout <<"coll1: " << endl;
for(pos1=coll1.begin();pos1!=coll1.end();++pos1) {
cout << *pos1 <<' ';
}
cout << endl;
coll2.resize(coll1.size());
copy(coll1.begin(),coll1.end(),coll2.begin());
cout <<"coll2: " << endl;
for(pos2=coll2.begin();pos2!=coll2.end();++pos2) {
cout << *pos2 <<' ';
}
cout << endl;
deque<int> coll3(coll1.size());
deque<int>::iterator pos3;
copy(coll1.begin(),coll1.end(),coll3.begin());
cout <<"coll3: " << endl;
for(pos3=coll3.begin();pos3!=coll3.end();++pos3) {
cout << *pos3 <<' ';
}
cout << endl;
}
檔名:vector101.cpp
檔案內容:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>
using namespace std;
int main() {
vector<string> sentence;
sentence.reserve(5);
sentence.push_back("Hello,");
sentence.push_back("how");
sentence.push_back("are");
sentence.push_back("you");
sentence.push_back("?");
copy (sentence.begin(),sentence.end(),ostream_iterator<string>(cout," "));
cout << endl;
cout << "max_size(): " << sentence.max_size() << endl;
cout << " size(): " << sentence.size() << endl;
cout << " capacity(): " << sentence.capacity() << endl;
swap (sentence[1],sentence[3]);
sentence.insert( find(sentence.begin(),sentence.end(),"?"),"always");
copy (sentence.begin(),sentence.end(),ostream_iterator<string>(cout," "));
cout << endl;
cout << "max_size(): " << sentence.max_size() << endl;
cout << " size(): " << sentence.size() << endl;
cout << " capacity(): " << sentence.capacity() << endl;
}
檔名:deque101.cpp
檔案內容:
#include <iostream>
#include <deque>
#include <string>
#include <algorithm>
#include <iterator>
using namespace std;
int main() {
deque<string> col1;
col1.assign(3,string("string"));
col1.push_back("last string");
col1.push_front("first string");
copy(col1.begin(),col1.end(),ostream_iterator<string>(cout,"\n"));
cout << endl;
col1.pop_front();
col1.pop_back();
copy(col1.begin(),col1.end(),ostream_iterator<string>(cout,"\n"));
cout << endl;
cout << col1.size();
cout << endl;
for (int i=1;i<col1.size();++i) {
col1[i] = "another " + col1[i];
}
col1.resize(4,"resized string");
copy(col1.begin(),col1.end(),ostream_iterator<string>(cout,"\n"));
}
檔名:map1.cpp
檔案內容:
檔名:map1.cpp
檔案內容:
沒有留言:
張貼留言