List methods

Table 6.12. Constructors and Destructor of Lists Operation Effect 
list<Elem> c Creates an empty list without any elements 
list<Elem> c1(c2) Creates a copy of another list of the same type (all elements are copied) 
list<Elem> c(n) Creates a list with n elements that are created by the default constructor 
list<Elem> c(n,elem) Creates a list initialized with n copies of element elem 
list<Elem> c (beg,end) Creates a list initialized with the elements of the range [beg,end) 
c.~list<Elem>() Destroys all elements and frees the 
memory 

Table 6.13. Nonmodifying Operations of Lists Operation Effect 
c.size() Returns the actual number of elements 
c. empty () Returns whether the container is empty (equivalent to size()==0, but might be faster) 
c.max_size() Returns the maximum number of elements possible 
c1 == c2 Returns whether c1 is equal to c2 
c1 != c2 Returns whether c1 is not equal to c2 (equivalent to ! (c1==c2)) 
c1 < c2 Returns whether c1 is less than c2 
c1 > c2 Returns whether c1 is greater than c2 (equivalent to c2<c1) 
c1 <= c2 Returns whether c1 is less than or equal to c2 (equivalent to ! (c2<c1) ) 
c1 >= c2 Returns whether c1 is greater than or equal to c2 (equivalent to ! (c1<c2)) 

Table 6.14. Assignment Operations of Lists Operation Effect 
c1 = c2 Assigns all elements of c2 to c1 
c.assign(n,elem) Assigns n copies of element elem 
c.assign(beg,end) Assigns the elements of the range [beg,end) 
c1.swap(c2) Swaps the data of c1 and c2 
swap(c1,c2) Same (as global function) 

Table 6.15. Direct Element Access of Lists Operation Effect 
c.front() Returns the first element (no check whether a first element exists) 
c.back() Returns the last element (no check whether a last element exists) 

Table 6.16. Iterator Operations of Lists Operation Effect 
c.begin() Returns a bidirectional iterator for the first element 
c.end() Returns a bidirectional iterator for the position after the last element 
c.rbegin() Returns a reverse iterator for the first element of a reverse iteration 
c.rend() Returns a reverse iterator for the position after the last element of a reverse iteration 

Table 6.17. Insert and Remove Operations of Lists Operation Effect 
c.insert (pos, elem) Inserts at iterator position pos a copy of elem and returns the position of the new element 
c.insert (pos,n, elem) Inserts at iterator position pos n copies of elem (returns nothing) 
c. insert (pos, beg,end) Inserts at iterator position pos a copy of all elements of the range [beg,end) (returns nothing) 
c.push_back(elem) Appends a copy of elem at the end 
c.pop_back() Removes the last element (does not return it) 
c.push_front(elem) Inserts a copy of elem at the beginning 
c.pop_front () Removes the first element (does not return it) 
c. remove (val) Removes all elements with value val 
c.remove_if (op) Removes all elements for which op(elem) yields true 
c. erase (pos) Removes the element at iterator position pos and returns the position of the next element 
c.erase (beg,end) Removes all elements of the range [beg,end) and returns the position of the next element 
c. resize (num) Changes the number of elements to num (if size() grows, new elements are created by their default constructor) 
c.resize (num, elem) Changes the number of elements to num (if size ( ) grows, new elements are copies of elem) 
c. clear () Removes all elements (makes the container empty) 

Table 6.18. Special Modifying Operations for Lists Operation Effect 
c.unique() Removes duplicates of consecutive elements with the same value 
c.unique(op) Removes duplicates of consecutive elements, for which op() yields true 
c1.splice(pos,c2) Moves all elements of c2 to c1 in front of the iterator position pos 
c1.splice(pos,c2,c2pos) Moves the element at c2pos in c2 in front of pos of list c1 (c1 and c2 may be identical) 
c1.splice(pos,c2,c2beg,c2end) Moves all elements of the range [c2beg,c2end) in c2 in front of pos of list c1 (c1 and c2 may be identical) 
c.sort() Sorts all elements with operator < 
c.sort(op) Sorts all elements with op() 
c1.merge(c2) Assuming both containers contain the elements sorted, moves all elements of c2 into c1 so that all elements are merged and still sorted 
c1.merge(c2,op) Assuming both containers contain the elements sorted due to the sorting criterion op(), moves all elements of c2 into c1 so that all elements are merged and still sorted according to op() 
c.reverse() Reverses the order of all elements 

Examples of Using Lists 
The following example in particular shows the use of the special member functions for lists: 


// cont/list1.cpp 

#include <iostream> 
#include <list> 
#include <algorithm> 
using namespace std; 

void printLists (const list<int>& 11, const list<int>& 12) 


cout << "list1: "; 
copy (l1.begin(), l1.end(), ostream_iterator<int>(cout," ")); 
cout << endl << "list2: "; 
copy (12.begin(), 12.end(), ostream_iterator<int>(cout," ")); 
cout << endl << endl; 



int main() 


//create two empty lists 
list<int> list1, list2; 

//fill both lists with elements 
for (int i=0; i<6; ++i) { 
list1.push_back(i); 
list2.push_front(i); 

printLists(list1, list2); 

//insert all elements of list1 before the first element with value 3 of list2 
//-find() returns an iterator to the first element with value 3 
list2.splice(find(list2.begin(),list2.end(), // destination position 
3), 
list1); // source list 
printLists(list1, list2); 

//move first element to the end 
list2.splice(list2.end(), // destination position 
list2, // source list 
list2.begin()); // source position 
printLists(list1, list2); 

//sort second list, assign to list1 and remove duplicates 
list2.sort(); 
list1 = list2; 
list2.unique(); 
printLists(list1, list2); 

//merge both sorted lists into the first list 
list1.merge(list2); 
printLists(list1, list2); 



The program has the following output: 


list1: 0 1 2 3 4 5 
list2: 5 4 3 2 1 0 

list1: 
list2: 5 4 0 1 2 3 4 5 3 2 1 0 

list1: 
list2: 4 0 1 2 3 4 5 3 2 1 0 5 

list1: 0 0 1 1 2 2 3 3 4 4 5 5 
list2: 0 1 2 3 4 5 

list1: 0 0 0 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 
list2: 


Vector的 
//建立一个向量并为之分配内存 
std::vector<int> v; // create an empty vector 
v.reserve (80); // reserve memory for 80 elements 
//建立一个向量,并用默认的构造函数初始化,因此速度较慢 
std::vector<T> v(5); // creates a vector and initializes it with five values 
// (calls five times the default constructor of type T) 


Table 6.2. Constructors and Destructors of Vectors Operation Effect 
vector<Elem> c Creates an empty vector without any elements 
vector<Elem> c1(c2) Creates a copy of another vector of the same type (all elements are copied) 
vector<Elem> c(n) Creates a vector with n elements that are created by the default constructor 
vector<Elem> c(n,elem) Creates a vector initialized with n copies of element elem 
vector<Elem> c(beg,end) Creates a vector initialized with the elements of the range [beg,end) 
c.~vector<Elem>() Destroys all elements and frees the memory 

Table 6.3. Nonmodifying Operations of Vectors Operation Effect 
c.size() Returns the actual number of elements 
c.empty() Returns whether the container is empty (equivalent to size()==0, but might be faster) 
c.max_size() Returns the maximum number of elements possible 
capacity() Returns the maximum possible number of elements without reallocation 
reserve() Enlarges capacity, if not enough yet[7] //如果不够的话就继续分配内存 
c1 == c2 Returns whether c1 is equal to c2 
c1 != c2 Returns whether c1 is not equal to c2 (equivalent to ! (c1==c2)) 
c1 < c2 Returns whether c1 is less than c2 
c1 > c2 Returns whether c1 is greater than c2 (equivalent to c2<c1) 
c1 <= c2 Returns whether c1 is less than or equal to c2 (equivalent to ! (c2<c1)) 
c1 >= c2 Returns whether c1 is greater than or equal to c2 (equivalent to ! (c1<c2)) 

Table 6.4. Assignment Operations of Vectors Operation Effect 
c1 = c2 Assigns all elements of c2 to c1 
c.assign(n,elem) Assigns n copies of element elem 
c.assign(beg,end) Assigns the elements of the range [beg,end) 
c1.swap(c2) Swaps the data of c1 and c2 
swap(c1,c2) Same (as global function) 

Table 6.5. Direct Element Access of Vectors Operation Effect 
c.at(idx) Returns the element with index idx (throws range error exception if idx is out of range) 
c[idx] Returns the element with index idx (no range checking) 
c.front() Returns the first element (no check whether a first element exists) 
c.back() Returns the last element (no check whether a last element exists) 

通过at来访问元素的时候如果越界会有一个out_of_range异常 
用[]重载来访问的时候只会报错 

Table 6.6. Iterator Operations of Vectors Operation Effect 
c.begin() Returns a random access iterator for the first element 
c.end() Returns a random access iterator for the position after the last element 
c.rbegin() Returns a reverse iterator for the first element of a reverse iteration 
c.rend() Returns a reverse iterator for the position after the last element of a reverse iteration 

Table 6.7. Insert and Remove Operations of Vectors Operation Effect 
c.insert(pos,elem) Inserts at iterator position pos a copy of elem and returns the position of the new element 
c.insert(pos,n,elem) Inserts at iterator position pos n copies of elem (returns nothing) 
c.insert(pos,beg,end) Inserts at iterator position pos a copy of all elements of the range [beg,end) (returns nothing) 
c.push_back(elem) Appends a copy of elem at the end 
c.pop_back() Removes the last element (does not return it) 
c.erase(pos) Removes the element at iterator position pos and returns the position of the next element 
c.erase(beg,end) Removes all elements of the range [beg,end) and returns the position of the next element 
c.resize(num) Changes the number of elements to num (if size() grows, new elements are created by their default constructor) 
c.resize(num,elem) Changes the number of elements to num (if size() grows, new elements are copies of elem) 
c.clear() Removes all elements (makes the container empty) 

std::vector<Elem> coll; 
... 
//remove all elements with value val 
coll.erase(remove(coll.begin(),coll.end(), 
val), 
coll.end()); 

std::vector<Elem> coll; 
... 
//remove first element with value val 
std::vector<Elem>::iterator pos; 
pos = find(coll.begin(),coll.end(), 
val); 
if (pos != coll.end()) { 
coll.erase(pos); 


vector<bool>有特殊的函数 
Table 6.8. Special Operations of vector<bool> Operation Effect 
c.flip() Negates all Boolean elements (complement of all bits) 
m[idx].flip() Negates the Boolean element with index idx (complement of a single bit) 
m[idx] = val Assigns val to the Boolean element with index idx (assignment to a single bit) 
m[idx1] = m[idx2] Assigns the value of the element with index idx2 to the element with index idx1 

Examples of Using Vectors 
The following example shows a simple usage of vectors: 


// cont/vector1.cpp 

#include <iostream> 
#include <vector> 
#include <string> 
#include <algorithm> 
using namespace std; 

int main() 


//create empty vector for strings 
vector<string> sentence; 

//reserve memory for five elements to avoid reallocation 
sentence.reserve(5); 

//append some elements 
sentence.push_back("Hello,"); 
sentence.push_back("how"); 
sentence.push_back("are"); 
sentence.push_back("you"); 
sentence.push_back("?"); 

//print elements separated with spaces 
copy (sentence.begin(), sentence.end(), 
ostream_iterator<string>(cout," ")); 
cout << endl; 

//print ''technical data'' 
cout << " max_size(): " << sentence.max_size() << endl; 
cout << " size(): " << sentence.size() << endl; 
cout << " capacity(): " << sentence.capacity() << endl; 

//swap second and fourth element 
swap (sentence[1], sentence [3]); 

//insert element "always" before element "?" 
sentence.insert (find(sentence.begin(),sentence.end(),"?"), 
"always"); 

//assign "!" to the last element 
sentence.back() = "!"; 

//print elements separated with spaces 
copy (sentence.begin(), sentence.end(), 
ostream_iterator<string>(cout," ")); 
cout << endl; 

//print "technical data" again 
cout << " max_size(): " << sentence.max_size() << endl; 
cout << " size(): " << sentence.size() << endl; 
cout << " capacity(): " << sentence.capacity() << endl; 




The output of the program might look like this: 


Hello, how are you ? 
max_size(): 268435455 
size(): 5 
capacity(): 5 
Hello, you are how always ! 
max_size(): 268435455 
size(): 6 
capacity(): 10

作者:码瘾少年·麒麟子 
出处:http://www.cnblogs.com/geniusalex/ 
蛮牛专栏:麒麟子 
简介:09年入行,喜欢游戏和编程,对3D游戏和引擎尤其感兴趣。 
版权声明:本文版权归作者和博客园共有,欢迎转载。转载必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

转载:http://www.cnblogs.com/geniusalex/archive/2009/03/31/1940527.html

下载方式:https://pan.quark.cn/s/a4b39357ea24 布线问题(分支限界算法)是计算机科学和电子工程领域中一个广为人知的议题,它主要探讨如何在印刷电路板上定位两个节点间最短的连接路径。 在这一议题中,电路板被构建为一个包含 n×m 个方格的矩阵,每个方格能够被界定为可通行或不可通行,其核心任务是定位从初始点到最终点的最短路径。 分支限界算法是处理布线问题的一种常用策略。 该算法与回溯法有相似之处,但存在差异,分支限界法仅需获取满足约束条件的一个最优路径,并按照广度优先或最小成本优先的原则来探索解空间树。 树 T 被构建为子集树或排列树,在探索过程中,每个节点仅被赋予一次成为扩展节点的机会,且会一次性生成其全部子节点。 针对布线问题的解决,队列式分支限界法可以被采用。 从起始位置 a 出发,将其设定为首个扩展节点,并将与该扩展节点相邻且可通行的方格加入至活跃节点队列中,将这些方格标记为 1,即从起始方格 a 到这些方格的距离为 1。 随后,从活跃节点队列中提取队首节点作为下一个扩展节点,并将与当前扩展节点相邻且未标记的方格标记为 2,随后将这些方格存入活跃节点队列。 这一过程将持续进行,直至算法探测到目标方格 b 或活跃节点队列为空。 在实现上述算法时,必须定义一个类 Position 来表征电路板上方格的位置,其成员 row 和 col 分别指示方格所在的行和列。 在方格位置上,布线能够沿右、下、左、上四个方向展开。 这四个方向的移动分别被记为 0、12、3。 下述表格中,offset[i].row 和 offset[i].col(i=0,1,2,3)分别提供了沿这四个方向前进 1 步相对于当前方格的相对位移。 在 Java 编程语言中,可以使用二维数组...
源码来自:https://pan.quark.cn/s/a4b39357ea24 在VC++开发过程中,对话框(CDialog)作为典型的用户界面组件,承担着与用户进行信息交互的重要角色。 在VS2008SP1的开发环境中,常常需要满足为对话框配置个性化背景图片的需求,以此来优化用户的操作体验。 本案例将系统性地阐述在CDialog框架下如何达成这一功能。 首先,需要在资源设计工具中构建一个新的对话框资源。 具体操作是在Visual Studio平台中,进入资源视图(Resource View)界面,定位到对话框(Dialog)分支,通过右键选择“插入对话框”(Insert Dialog)选项。 完成对话框内控件的布局设计后,对对话框资源进行保存。 随后,将着手进行背景图片的载入工作。 通常有两种主要的技术路径:1. **运用位图控件(CStatic)**:在对话框界面中嵌入一个CStatic控件,并将其属性设置为BST_OWNERDRAW,从而具备自主控制绘制过程的权限。 在对话框的类定义中,需要重写OnPaint()函数,负责调用图片资源并借助CDC对象将其渲染到对话框表面。 此外,必须合理处理WM_CTLCOLORSTATIC消息,确保背景图片的展示不会受到其他界面元素的干扰。 ```cppvoid CMyDialog::OnPaint(){ CPaintDC dc(this); // 生成设备上下文对象 CBitmap bitmap; bitmap.LoadBitmap(IDC_BITMAP_BACKGROUND); // 获取背景图片资源 CDC memDC; memDC.CreateCompatibleDC(&dc); CBitmap* pOldBitmap = m...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值