0
点赞
收藏
分享

微信扫一扫

vector::assign


Assign vector content:

Assigns new contents to the vector, replacing its current contents, and modifying its size accordingly.

功能:

将区间[begin,end]的元素赋值到当前的vector容器中,或者赋n个值为x的元素到vector容器中,这个容器会清除掉vector容器中以前的内容。

// vector assign(分配):将新内容分配给vector,替换其当前内容,并相应地修改它的大小。
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
std::vector<int> first;
std::vector<int> second;
std::vector<int> third;

first.assign (7,100); // 7 ints with a value of 100

vector<int>::iterator it;
it=first.begin()+1; //后一个


cout<<*it<<endl; //输出 100

second.assign (it , first.end()-1); // the 5 central values of first

int myints[] = {2016,7,4};
third.assign (myints,myints+3); // assigning from array.

std::cout << "Size of first: " << int (first.size()) << '\n'; // 大小为:7
std::cout << "Size of second: " << int (second.size()) << '\n'; // 大小为:5
std::cout << "Size of third: " << int (third.size()) << '\n'; // 大小为:3
return 0;
}



vector::assign_assign



举报

相关推荐

0 条评论