Vector

简介: 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.
1.string str[]={"Alex","John","Robert"};
// creates vector with 10 elements,
 // and assign value 0 for each 
vector<int> v3(10,0);
vector<string> v4(str+0,str+3); 
vector<string>::iterator sIt = v4.begin(); 
while ( sIt != v4.end() ) 
cout << *sIt++ << " "; 
cout << endl; 
// copy constructor 
vector<string> v5(v4); 
for ( int i=0; i<3; i++)
cout << v5[i] << " "; 
cout << endl; 
return 0; 
} 
OUTPUT: 
// Alex John Robert 
// Alex John Robert




1、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 <iostream> 
#include <vector> 
using namespace std; 
int main () 
{ 
vector<int> 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 <iostream> 
#include <vector> 
#include <iterator>
 #include <numeric> //iota
using namespace std; 
int main () 
{ 
vector<int> v(5); 
iota(v.begin(),v.end(),1);//stl自增函数 
vector<int>::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 <iostream>
 #include <vector> 
using namespace std; 
int main () 
{ 
vector<int> 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 = 100



2.fill(v.begin(),v.end(),5);

v.clear();//清除vector内容


3.#include <iostream> 
#include <vector> 
using namespace std; 
int main () 
{ 
vector<int> 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 empty


4.
#include <iostream> 
#include <vector> 
#include <iterator> 
#include <numeric> 
using namespace std; 
int main () 
{ 
vector<int> v(5);
iota(v.begin(),v.end(),1); 
vector<int>::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 
// 5

5.
#include <vector>
#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
    vector<int> vect;    
    vect.push_back(1);
    vect.push_back(2);
    vect.push_back(3);
    vect.push_back(4);
    vect.resize(100);    //新的空间不覆盖原有四个元素占有的空间,现在size和

capacity都是100
    cout<<vect.size()<<endl;
    int i = 0;
    for (i = 0; i < 104; i++)
    {
        cout<<vect[i]<<endl;  
    }
    return 0;
}

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

int main(int argc, char* argv[])
{
    vector<int> vect;        
    vect.resize(100);    //分配100个空间
        vect.push_back(1);
    vect.push_back(2);
    vect.push_back(3);
    vect.push_back(4);
    cout<<vect.size()<<endl; //现在size和capacity都是104
    int i = 0;
    for (i = 0; i < 104; i++)
    {
        cout<<vect[i]<<endl;  
    }
    return 0;
}


6.
#include <iostream> 
#include <vector> 
using namespace std; 
int main () 
{
 vector<int> 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 = 1073741823


7.pop_back() 删除最后一个元素

template <class T> 
class Print 
{ public: 
void operator () (T& t) 
{ cout << t << " "; } }; 
//============================= 
int main () 
{ 
vector<int> v; 
Print<int> 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 
// 1



8.
#include <iostream> 
#include <vector> 
using namespace std; 
int main () 
{ 
vector<int> 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 = 12

9.

#include <iostream> 
#include <vector> 
#include <algorithm> 
#include <iterator> 
using namespace std; 
int main () 
{ 
vector<int> v(5); 
for ( int i=0; i<5; i++ ) 
v[i] = i*2; 
copy(v.begin(),v.end(), ostream_iterator<int>(cout," "));
 cout << endl; 
v.resize(7,100); 
copy(v.begin(),v.end(), ostream_iterator<int>(cout," "));
cout << endl; v.resize(4); 
copy(v.begin(),v.end(), ostream_iterator<int>(cout," "));
 cout << endl; 
return 0; 
}
 OUTPUT: // 0 2 4 6 8 
// 0 2 4 6 8 100 100
 // 0 2 4 6


10.

  

目录
相关文章
|
6月前
|
消息中间件 Java 数据库
RocketMQ实战—9.营销系统代码初版
本文主要介绍了实现营销系统四大促销场景的代码初版:全量用户推送促销活动、全量用户发放优惠券、特定用户推送领取优惠券消息、热门商品定时推送。
RocketMQ实战—9.营销系统代码初版
|
6月前
|
存储 人工智能 分布式计算
阿里云服务器实例规格选择参考:如何根据业务场景选择适合自己的实例规格
在我们购买阿里云服务器的时候,阿里云提供了众多的云服务器实例规格,满足了不同行业、不同业务场景的多样化需求。然而,面对众多的实例选择,如何根据自身的业务特性,挑选出最合适的云服务器实例规格,成为了众多用户,尤其是新手用户比较关心的问题。本文旨在通过深入剖析阿里云服务器的各类实例规格,结合具体的业务场景,为您提供一份详尽的实例规格选择指南,以供参考和选择。
|
机器学习/深度学习 数据采集 算法
深度学习和机器学习中针对非时间序列的回归任务,有哪些改进角度?
本文探讨了在深度学习和机器学习中针对非时间序列的回归任务的多种改进策略,包括数据预处理、数据集增强、特征选择、模型选择、模型正则化与泛化、优化器选择、学习率调整、超参数调优以及性能评估与模型解释,旨在提升模型的性能和可解释性。
271 1
深度学习和机器学习中针对非时间序列的回归任务,有哪些改进角度?
|
SpringCloudAlibaba Java Nacos
客户端启动报错java.lang.IllegalArgumentException: no server available的解决方案 SpringCloud中 Nacos做注册中心
客户端启动报错java.lang.IllegalArgumentException: no server available的解决方案 SpringCloud中 Nacos做注册中心
592 0
|
12月前
|
存储 监控 固态存储
如何在 Linux 上检查 SSD/HDD 健康状况?
【10月更文挑战第14天】
1106 1
如何在 Linux 上检查 SSD/HDD 健康状况?
|
消息中间件 缓存 Shell
跟我一起来学OpenStack部署
跟我一起来学OpenStack部署
633 0
|
JavaScript 前端开发 开发者
Vue3使用hook封装媒体查询和事件监听,使Vue的开发更加丝滑🚀🚀🚀
Vue3使用hook封装媒体查询和事件监听,使Vue的开发更加丝滑🚀🚀🚀
|
分布式计算 Ubuntu Hadoop
大数据基础系列 4:伪分布式 Hadoop 在 Ubuntu 上的安装流程完整步骤及易错点分析
大数据基础系列 4:伪分布式 Hadoop 在 Ubuntu 上的安装流程完整步骤及易错点分析
428 0
大数据基础系列 4:伪分布式 Hadoop 在 Ubuntu 上的安装流程完整步骤及易错点分析
|
C语言 C++
C++ 初阶 文件操作和io流
C++ 初阶 文件操作和io流
237 0
C++ 初阶 文件操作和io流
|
jenkins Java 测试技术