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

本文共 5280 字,大约阅读时间需要 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.

  

转载于:https://www.cnblogs.com/hxsyl/archive/2012/08/05/2623916.html

你可能感兴趣的文章
Java_Activiti5_菜鸟也来学Activiti5工作流_之入门简单例子(一)
查看>>
设计模式(一)工厂模式Factory(创建型)
查看>>
linux中安装软件的集中方法
查看>>
Express中间件,看这篇文章就够了(#^.^#)
查看>>
《构建之法》(五)
查看>>
创建django项目
查看>>
Linux Bash基本功能
查看>>
一则小脚本(工作中用)
查看>>
软件工程结对作业
查看>>
Keil 4.0 生成bin文件
查看>>
sql语句的进化--hibernate篇
查看>>
python爬虫之cookie
查看>>
2017年5月29号课堂笔记
查看>>
HDU4247【瞎搞】
查看>>
lightoj 1125【背包·从n个选m个】
查看>>
HDU 1243 反恐训练营(最长公共序列)
查看>>
mysql数据库隔离级别
查看>>
(六)buildroot使用详解
查看>>
chrome修改UserAgent,调试
查看>>
Source Insight4.0 试用。。试用。。试用。。
查看>>