王朝知道
分享
 
 
 

C++ 的list,和vector里的函数是怎么用的

王朝知道·作者佚名  2010-04-24  
宽屏版  字体: |||超大  
 
分类: 电脑/网络 >> 程序设计 >> 其他编程语言
 
问题描述:

list ,vector 里的函数是怎么用的?

是系统里自己定义的,有什么push_back()等的,请讲详细点,谢谢了

参考答案:

使用方法

#include <vector>

#include <list>

using namespace std;

然后直接使用

List的所有函数 全英文.....

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(): ***********

size(): 5

capacity(): 5

Hello, you are how always !

max_size(): ***********

size(): 6

capacity(): 10

小贴士:① 若网友所发内容与教科书相悖,请以教科书为准;② 若网友所发内容与科学常识、官方权威机构相悖,请以后者为准;③ 若网友所发内容不正确或者违背公序良俗,右下举报/纠错。
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
如何用java替换看不见的字符比如零宽空格&#8203;十六进制U+200B
 干货   2023-09-10
网页字号不能单数吗,网页字体大小为什么一般都是偶数
 干货   2023-09-06
java.lang.ArrayIndexOutOfBoundsException: 4096
 干货   2023-09-06
Noto Sans CJK SC字体下载地址
 干货   2023-08-30
window.navigator和navigator的区别是什么?
 干货   2023-08-23
js获取referer、useragent、浏览器语言
 干货   2023-08-23
oscache遇到404时会不会缓存?
 干货   2023-08-23
linux下用rm -rf *删除大量文件太慢怎么解决?
 干货   2023-08-08
刀郎新歌破世界纪录!
 娱乐   2023-08-01
js实现放大缩小页面
 干货   2023-07-31
生成式人工智能服务管理暂行办法
 百态   2023-07-31
英语学习:过去完成时The Past Perfect Tense举例说明
 干货   2023-07-31
Mysql常用sql命令语句整理
 干货   2023-07-30
科学家复活了46000年前的虫子
 探索   2023-07-29
英语学习:过去进行时The Past Continuous Tense举例说明
 干货   2023-07-28
meta name="applicable-device"告知页面适合哪种终端设备:PC端、移动端还是自适应
 干货   2023-07-28
只用css如何实现打字机特效?
 百态   2023-07-15
css怎么实现上下滚动
 干货   2023-06-28
canvas怎么画一个三角形?
 干货   2023-06-28
canvas怎么画一个椭圆形?
 干货   2023-06-28
canvas怎么画一个圆形?
 干货   2023-06-28
canvas怎么画一个正方形?
 干货   2023-06-28
中国河南省郑州市金水区蜘蛛爬虫ip大全
 干货   2023-06-22
javascript简易动态时间代码
 干货   2023-06-20
感谢员工的付出和激励的话怎么说?
 干货   2023-06-18
 
>>返回首页<<
 
 
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有