C++编程思想 第2卷 第7章 通用容器 基本序列容器:vector list deque 基本序列容器的操作
所有基本序列容器完全按照存进去时的顺序持有对象
然而 对于不同的基本序列容器
它们的操作效率是不同的
因此如果想要操作具有某种特点的序列
则应当针对不同的操作类型选择合适的容器
演示了所有基本序列容器 vector deque list所支持的操作
//: C07:BasicSequenceOperations.cpp
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file 'License.txt',
// distributed with the code package available at www.MindView.net.
// The operations available for all the
// basic sequence Containers.
#include <deque>
#include <iostream>
#include <list>
#include <vector>
using namespace std;
template<typename Container>
void print(Container& c, char* title = "") {
cout << title << ':' << endl;
if(c.empty()) {
cout << "(empty)" << endl;
return;
}
typename Container::iterator it;
for(it = c.begin(); it != c.end(); it++)
cout << *it << " ";
cout << endl;
cout << "size() " << c.size()
<< " max_size() " << c.max_size()
<< " front() " << c.front()
<< " back() " << c.back()
<< endl;
}
template<typename ContainerOfInt> void basicOps(char* s) {
cout << "------- " << s << " -------" << endl;
typedef ContainerOfInt Ci;
Ci c;
print(c, "c after default constructor");
Ci c2(10, 1); // 10 elements, values all 1
print(c2, "c2 after constructor(10,1)");
int ia[] = { 1, 3, 5, 7, 9 };
const int IASZ = sizeof(ia)/sizeof(*ia);
// Initialize with begin & end iterators:
Ci c3(ia, ia + IASZ);
print(c3, "c3 after constructor(iter,iter)");
Ci c4(c2); // Copy-constructor
print(c4, "c4 after copy-constructor(c2)");
c = c2; // Assignment operator
print(c, "c after operator=c2");
c.assign(10, 2); // 10 elements, values all 2
print(c, "c after assign(10, 2)");
// Assign with begin & end iterators:
c.assign(ia, ia + IASZ);
print(c, "c after assign(iter, iter)");
cout << "c using reverse iterators:" << endl;
typename Ci::reverse_iterator rit = c.rbegin();
while(rit != c.rend())
cout << *rit++ << " ";
cout << endl;
c.resize(4);
print(c, "c after resize(4)");
c.push_back(47);
print(c, "c after push_back(47)");
c.pop_back();
print(c, "c after pop_back()");
typename Ci::iterator it = c.begin();
++it; ++it;
c.insert(it, 74);
print(c, "c after insert(it, 74)");
it = c.begin();
++it;
c.insert(it, 3, 96);
print(c, "c after insert(it, 3, 96)");
it = c.begin();
++it;
c.insert(it, c3.begin(), c3.end());
print(c, "c after insert("
"it, c3.begin(), c3.end())");
it = c.begin();
++it;
c.erase(it);
print(c, "c after erase(it)");
typename Ci::iterator it2 = it = c.begin();
++it;
++it2; ++it2; ++it2; ++it2; ++it2;
c.erase(it, it2);
print(c, "c after erase(it, it2)");
c.swap(c2);
print(c, "c after swap(c2)");
c.clear();
print(c, "c after clear()");
}
int main() {
basicOps<vector<int> >("vector");
basicOps<deque<int> >("deque");
basicOps<list<int> >("list");
getchar();
} ///:~
输出
------- vector -------
c after default constructor:
(empty)
c2 after constructor(10,1):
1 1 1 1 1 1 1 1 1 1
size() 10 max_size() 1073741823 front() 1 back() 1
c3 after constructor(iter,iter):
1 3 5 7 9
size() 5 max_size() 1073741823 front() 1 back() 9
c4 after copy-constructor(c2):
1 1 1 1 1 1 1 1 1 1
size() 10 max_size() 1073741823 front() 1 back() 1
c after operator=c2:
1 1 1 1 1 1 1 1 1 1
size() 10 max_size() 1073741823 front() 1 back() 1
c after assign(10, 2):
2 2 2 2 2 2 2 2 2 2
size() 10 max_size() 1073741823 front() 2 back() 2
c after assign(iter, iter):
1 3 5 7 9
size() 5 max_size() 1073741823 front() 1 back() 9
c using reverse iterators:
9 7 5 3 1
c after resize(4):
1 3 5 7
size() 4 max_size() 1073741823 front() 1 back() 7
c after push_back(47):
1 3 5 7 47
size() 5 max_size() 1073741823 front() 1 back() 47
c after pop_back():
1 3 5 7
size() 4 max_size() 1073741823 front() 1 back() 7
c after insert(it, 74):
1 3 74 5 7
size() 5 max_size() 1073741823 front() 1 back() 7
c after insert(it, 3, 96):
1 96 96 96 3 74 5 7
size() 8 max_size() 1073741823 front() 1 back() 7
c after insert(it, c3.begin(), c3.end()):
1 1 3 5 7 9 96 96 96 3 74 5 7
size() 13 max_size() 1073741823 front() 1 back() 7
c after erase(it):
1 3 5 7 9 96 96 96 3 74 5 7
size() 12 max_size() 1073741823 front() 1 back() 7
c after erase(it, it2):
1 96 96 96 3 74 5 7
size() 8 max_size() 1073741823 front() 1 back() 7
c after swap(c2):
1 1 1 1 1 1 1 1 1 1
size() 10 max_size() 1073741823 front() 1 back() 1
c after clear():
(empty)
------- deque -------
c after default constructor:
(empty)
c2 after constructor(10,1):
1 1 1 1 1 1 1 1 1 1
size() 10 max_size() 1073741823 front() 1 back() 1
c3 after constructor(iter,iter):
1 3 5 7 9
size() 5 max_size() 1073741823 front() 1 back() 9
c4 after copy-constructor(c2):
1 1 1 1 1 1 1 1 1 1
size() 10 max_size() 1073741823 front() 1 back() 1
c after operator=c2:
1 1 1 1 1 1 1 1 1 1
size() 10 max_size() 1073741823 front() 1 back() 1
c after assign(10, 2):
2 2 2 2 2 2 2 2 2 2
size() 10 max_size() 1073741823 front() 2 back() 2
c after assign(iter, iter):
1 3 5 7 9
size() 5 max_size() 1073741823 front() 1 back() 9
c using reverse iterators:
9 7 5 3 1
c after resize(4):
1 3 5 7
size() 4 max_size() 1073741823 front() 1 back() 7
c after push_back(47):
1 3 5 7 47
size() 5 max_size() 1073741823 front() 1 back() 47
c after pop_back():
1 3 5 7
size() 4 max_size() 1073741823 front() 1 back() 7
c after insert(it, 74):
1 3 74 5 7
size() 5 max_size() 1073741823 front() 1 back() 7
c after insert(it, 3, 96):
1 96 96 96 3 74 5 7
size() 8 max_size() 1073741823 front() 1 back() 7
c after insert(it, c3.begin(), c3.end()):
1 1 3 5 7 9 96 96 96 3 74 5 7
size() 13 max_size() 1073741823 front() 1 back() 7
c after erase(it):
1 3 5 7 9 96 96 96 3 74 5 7
size() 12 max_size() 1073741823 front() 1 back() 7
c after erase(it, it2):
1 96 96 96 3 74 5 7
size() 8 max_size() 1073741823 front() 1 back() 7
c after swap(c2):
1 1 1 1 1 1 1 1 1 1
size() 10 max_size() 1073741823 front() 1 back() 1
c after clear():
(empty)
------- list -------
c after default constructor:
(empty)
c2 after constructor(10,1):
1 1 1 1 1 1 1 1 1 1
size() 10 max_size() 357913941 front() 1 back() 1
c3 after constructor(iter,iter):
1 3 5 7 9
size() 5 max_size() 357913941 front() 1 back() 9
c4 after copy-constructor(c2):
1 1 1 1 1 1 1 1 1 1
size() 10 max_size() 357913941 front() 1 back() 1
c after operator=c2:
1 1 1 1 1 1 1 1 1 1
size() 10 max_size() 357913941 front() 1 back() 1
c after assign(10, 2):
2 2 2 2 2 2 2 2 2 2
size() 10 max_size() 357913941 front() 2 back() 2
c after assign(iter, iter):
1 3 5 7 9
size() 5 max_size() 357913941 front() 1 back() 9
c using reverse iterators:
9 7 5 3 1
c after resize(4):
1 3 5 7
size() 4 max_size() 357913941 front() 1 back() 7
c after push_back(47):
1 3 5 7 47
size() 5 max_size() 357913941 front() 1 back() 47
c after pop_back():
1 3 5 7
size() 4 max_size() 357913941 front() 1 back() 7
c after insert(it, 74):
1 3 74 5 7
size() 5 max_size() 357913941 front() 1 back() 7
c after insert(it, 3, 96):
1 96 96 96 3 74 5 7
size() 8 max_size() 357913941 front() 1 back() 7
c after insert(it, c3.begin(), c3.end()):
1 1 3 5 7 9 96 96 96 3 74 5 7
size() 13 max_size() 357913941 front() 1 back() 7
c after erase(it):
1 3 5 7 9 96 96 96 3 74 5 7
size() 12 max_size() 357913941 front() 1 back() 7
c after erase(it, it2):
1 96 96 96 3 74 5 7
size() 8 max_size() 357913941 front() 1 back() 7
c after swap(c2):
1 1 1 1 1 1 1 1 1 1
size() 10 max_size() 357913941 front() 1 back() 1
c after clear():
(empty)
第1个函数模板,print(),演示了能够从任何序列容器中得到的基本信息
容器时候为空
容器当前大小
容器的最大可能尺寸
其实元素和终止元素等
上一篇: PHP5.3中的静态变量