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.

  

目录
相关文章
|
7月前
|
存储 算法 测试技术
C++:Vector的使用
C++:Vector的使用
|
7月前
|
存储 编译器 C语言
vector讲解
vector讲解
77 0
|
7月前
|
编译器 C++
【c++】vector
【c++】vector
52 0
|
2月前
|
存储 编译器 C++
C++之打造my vector篇(上)
C++之打造my vector篇(上)
32 0
|
2月前
|
算法 C++ 容器
【C++】—— vector使用
【C++】—— vector使用
|
4月前
|
安全 Java
Vector的使用
Vector的使用
21 2
|
4月前
|
存储 算法 C语言
【C++】vector的认识与使用
【C++】vector的认识与使用
|
5月前
|
存储 C语言 C++
【C++】vector的使用上
**C++ STL的vector简介与用法:** Vector是动态顺序数组,提供高效下标访问,支持动态增长。与数组类似但可自动调整大小。常用构造函数包括默认、填充、迭代器范围和拷贝构造。析构函数自动释放内存。赋值运算符执行深拷贝。迭代器接口包括`begin()`和`end()`(反向对应`rbegin()`和`rend()`),C++11增加了const版本以支持只读访问。示例代码展示了不同构造函数和迭代器的使用。
|
5月前
|
编译器 C++
【C++】vector的使用下
**C++ 中的 `std::vector` 概要:** - **元素获取:** 支持 `operator[]`(越界时不检
|
7月前
|
算法 编译器 C++
(C++)vector介绍及其使用
(C++)vector介绍及其使用
70 0