博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Vector
阅读量:6854 次
发布时间:2019-06-26

本文共 5214 字,大约阅读时间需要 17 分钟。

1.string str[]={"Alex","John","Robert"};// creates vector with 10 elements, // and assign value 0 for each vector
v3(10,0);vector
v4(str+0,str+3); vector
::iterator sIt = v4.begin(); while ( sIt != v4.end() ) cout << *sIt++ << " "; cout << endl; // copy constructor vector
v5(v4); for ( int i=0; i<3; i++)cout << v5[i] << " "; cout << endl; return 0; } OUTPUT: // Alex John Robert // Alex John Robert1、string s1( "Mississippi" ); string s3;   // 拷贝s1 的前4 个字符   s3.assign( s1, 0, 4 );   s3 现在的值为“Miss”。   2、 string str1, str2 = "War and Peace";   str1.assign( str2, 4, 3 );   cout << str1 << endl;   显示   and#include
#include
using namespace std; int main () { vector
v(3,0); v[0] = 100; v.at(1) = 200; for ( int i=0; i<3; i++ ) cout << v.at(i) << " "; cout << endl; return 0; } OUTPUT: // 100 200 0#include
#include
#include
#include
//iotausing namespace std; int main () { vector
v(5); iota(v.begin(),v.end(),1);//stl自增函数 vector
::iterator It = v.begin(); while ( It != v.end() )cout << *It++ << " "; cout << endl; // third element of the vector It = v.begin()+2; cout << *It << endl; return 0;} OUTPUT: // 1 2 3 4 5 // 3  1.#include
#include
using namespace std; int main () { vector
v(10); cout << "Size of v = " << v.size() << endl; cout << "Capacity of v = " << v.capacity() << endl; v.resize(100); cout << "After resizing:" << endl; cout << "Size of v = " << v.size() << endl; cout << "Capacity of v = " << v.capacity() << endl; return 0;} OUTPUT: // Size of v = 10 // Capacity of v = 10 // After resizing: // Size of v = 100 // Capacity of v = 1002.fill(v.begin(),v.end(),5);v.clear();//清除vector内容3.#include
#include
using namespace std; int main () { vector
v; cout << "Vector is "; v.empty() ? cout << "" : cout << "not "; cout << "empty" << endl; //如c++中的vector头文件里面就有这个push_back函数,在vector类中作用为在vector尾部加入一个数据。v.push_back(100); cout << "Vector is "; v.empty() ? cout << "" : cout << "not "; cout << "empty" << endl; return 0; } // Vector is empty // Vector is not empty4.#include
#include
#include
#include
using namespace std; int main () { vector
v(5);iota(v.begin(),v.end(),1); vector
::iterator It = v.begin(); while ( It != v.end() ) cout << *It++ << " "; cout << endl; // last element of the vector It = v.end()-1; cout << *It << endl; return 0; } OUTPUT: // 1 2 3 4 5 // 55.#include
#include
using namespace std;int main(int argc, char* argv[]){ vector
vect; vect.push_back(1); vect.push_back(2); vect.push_back(3); vect.push_back(4); vect.resize(100); //新的空间不覆盖原有四个元素占有的空间,现在size和capacity都是100 cout<
<
#include
using namespace std;int main(int argc, char* argv[]){ vector
vect; vect.resize(100); //分配100个空间 vect.push_back(1); vect.push_back(2); vect.push_back(3); vect.push_back(4); cout<
<
#include
using namespace std; int main () { vector
v(10); cout << "Size of v = " << v.size() << endl; cout << "Max_size of v = " << v.max_size() << endl; return 0; } OUTPUT: // Size of v = 10 // Max_size of v = 10737418237.pop_back() 删除最后一个元素template
class Print { public: void operator () (T& t) { cout << t << " "; } }; //============================= int main () { vector
v; Print
print; for ( int i=0; i<5; i++ ) v.push_back(i+1); while ( !v.empty() ) { for_each(v.begin(),v.end(),print); cout << endl; v.pop_back(); } return 0; } OUTPUT: // 1 2 3 4 5 // 1 2 3 4 // 1 2 3 // 1 2 // 18.#include
#include
using namespace std; int main () { vector
v(5,0); // 5 elements, each - value 0 /*------------------------------------------------*/ cout << "Size of v = " << v.size() << endl; cout << "Capacity v = " << v.capacity() << endl; cout << "Value of each element is - "; for ( int i = 0; i < v.size(); i++ ) cout << v[i] << " "; cout << endl; v[0] = 5; // new value for first element v[1] = 8; v.push_back(3); // creates new (6th) element of vector, v.push_back(7); // automatically increases size cout << endl; // capacity of vector v cout << "Size of v = " << v.size() << endl; cout << "Capacity v = " << v.capacity() << endl; cout << "Value of each element is - "; for ( int i = 0; i < v.size(); i++ )cout << v[i] << " ";cout << endl << endl; v.reserve(100); // increase capacity to 100 cout << "Size of v1_int = " << v.size() << endl; cout << "Capacity v1_int = " << v.capacity() << endl; int size = sizeof(v); // how big is vector itself cout << "sizeof v = " << size << endl; return 0; } // Size of v = 5 // Capacity v = 5//返回当前vector在重新进行内存分配以前所能容纳的元素数量。 // Value of each element is - 0 0 0 0 0 // // Size of v = 7 // Capacity v = 10 // Value of each element is - 5 8 0 0 0 3 7 // // Size of v = 7 // Capacity v = 100 // sizeof v = 129.#include
#include
#include
#include
using namespace std; int main () { vector
v(5); for ( int i=0; i<5; i++ ) v[i] = i*2; copy(v.begin(),v.end(), ostream_iterator
(cout," ")); cout << endl; v.resize(7,100); copy(v.begin(),v.end(), ostream_iterator
(cout," "));cout << endl; v.resize(4); copy(v.begin(),v.end(), ostream_iterator
(cout," ")); cout << endl; return 0; } OUTPUT: // 0 2 4 6 8 // 0 2 4 6 8 100 100 // 0 2 4 610.

  

转载地址:http://llyyl.baihongyu.com/

你可能感兴趣的文章
Composer 中国镜像地址配置
查看>>
比特币暴跌后的连锁反应
查看>>
组件开发实战-组件分类与组件设计原则
查看>>
Python爬虫入门教程 62-100 30岁了,想找点文献提高自己,还被反爬了,Python搞起,反爬第2篇...
查看>>
第80节:Java中的MVC设计模式
查看>>
区块链100讲:以实例形式深入浅出讲透BANCOR算法
查看>>
Java并发编程 深入剖析volatile关键字
查看>>
Vue基础
查看>>
Flutter(一)之Flutter的的简单入门分析
查看>>
【Vue源码学习】Virtual Dom 原理解析
查看>>
js 中有哪些拷贝的方式
查看>>
k8s简单了解
查看>>
Quartz学习-通过binlog分析Quartz启动及集群的Failover
查看>>
当下流行的UI框架
查看>>
Python从零开始系列连载(21)——Python特色数据类型(元组)(下)
查看>>
[掘金专题] Google I/O 2017 已经结束,我们该如何评价?
查看>>
【适合新手入门计算机行业】计算机科学速成课-中英双语全40集
查看>>
【iOS报错】“The operation couldn’t be completed (LaunchServicesError erro
查看>>
深入剖析Vue源码 - 选项合并(下)
查看>>
vue父、子、孙组件间数据传递、事件传递
查看>>