vector或许是实际过程中使用最多的stl容器。看似简单,事实上有非常多技巧和陷阱。

着重看一看vector的构造,临时依照C++11:

default (1)
explicit vector (const allocator_type& alloc = allocator_type()); fill (2)
explicit vector (size_type n);
vector (size_type n, const value_type& val,
const allocator_type& alloc = allocator_type()); range (3)
template <class InputIterator>
vector (InputIterator first, InputIterator last,
const allocator_type& alloc = allocator_type()); copy (4)
vector (const vector& x);
vector (const vector& x, const allocator_type& alloc); move (5)
vector (vector&& x);
vector (vector&& x, const allocator_type& alloc); initializer list (6)
vector (initializer_list<value_type> il,
const allocator_type& alloc = allocator_type());

直接看看以下的代码,就知道怎样构造一个vector了:

#include <iostream>
#include <vector> int main ()
{ std::vector<int> first; // default (1)
std::vector<int> second (4,100); // fill (2)
std::vector<int> third (second.begin(),second.end()); // range (3)
std::vector<int> fourth (third); // a copy of third // the iterator constructor can also be used to construct from arrays:
int myints[] = {16,2,77,29};
std::vector<int> fifth (myints, myints + sizeof(myints) / sizeof(int) ); std::cout << "The contents of fifth are:";
for (std::vector<int>::iterator it = fifth.begin(); it != fifth.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n'; return 0;
}

===========================================================

vector重载了=运算符,也有一个叫assign的方法,并且有什么差别吗?

std::vector::operator=

直接代码:

#include <iostream>
#include <vector> int main ()
{
std::vector<int> foo (3,0);
std::vector<int> bar (5,0); bar = foo;
foo = std::vector<int>(); std::cout << "Size of foo: " << int(foo.size()) << '\n';
std::cout << "Size of bar: " << int(bar.size()) << '\n';
return 0;
} //结果:
Size of foo: 0
Size of bar: 3

这里须要说明的是:

replacing its current contents

modifying its size accordingly

std::vector::assign

相同直接代码:

#include <iostream>
#include <vector> 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 std::vector<int>::iterator it;
it=first.begin()+1; second.assign (it,first.end()-1); // the 5 central values of first int myints[] = {1776,7,4};
third.assign (myints,myints+3); // assigning from array. std::cout << "Size of first: " << int (first.size()) << '\n';
std::cout << "Size of second: " << int (second.size()) << '\n';
std::cout << "Size of third: " << int (third.size()) << '\n';
return 0;
}
//输出:
Size of first: 7
Size of second: 5
Size of third: 3

这里相同须要说明:

replacing its current contents

modifying its size accordingly

实战c++中的vector系列--构造、operator=和assign差别的更多相关文章

  1. 实战c++中的vector系列--再谈vector的insert()方法(都是make_move_iterator惹的祸)

    之前说过了关于vector的insert()方法,把vector B的元素插入到vector A中.vector A中的结果我们可想而知,可是vector B中的元素还会怎样? 看看之前写过的程序: ...

  2. 实战c++中的vector系列--vector的遍历(stl算法、vector迭代器(不要在循环中推断不等于end())、operator[])

    遍历一个vector容器有非常多种方法.使用起来也是仁者见仁. 通过索引遍历: for (i = 0; i<v.size(); i++) { cout << v[i] << ...

  3. 实战c++中的vector系列--知道emplace_back为何优于push_back吗?

    上一篇博客说道vector中放入struct.我们先构造一个struct对象.再push_back. 那段代码中,之所以不能使用emplace_back,就是由于我们定义的struct没有显示的构造函 ...

  4. 实战c++中的vector系列--vector应用之STL的find、find_if、find_end、find_first_of、find_if_not(C++11)

    使用vector容器,即避免不了进行查找,所以今天就罗列一些stl的find算法应用于vector中. find() Returns an iterator to the first element ...

  5. 实战c++中的vector系列--creating vector of local structure、vector of structs initialization

    之前一直没有使用过vector<struct>,如今就写一个简短的代码: #include <vector> #include <iostream> int mai ...

  6. 实战c++中的vector系列--copy set to vector(别混淆了reserve和resize)

    stl算法中有个copy函数.我们能够轻松的写出这种代码: #include <iostream> #include <algorithm> #include <vect ...

  7. 实战c++中的vector系列--将迭代器转换为索引

    stl的迭代器非常方便 用于各种算法. 可是一想到vector.我们总是把他当做数组,总喜欢使用下标索引,而不是迭代器. 这里有个问题就是怎样把迭代器转换为索引: #include <vecto ...

  8. 实战c++中的vector系列--正确释放vector的内存(clear(), swap(), shrink_to_fit())

    关于vector已经写的差不多了,似乎要接近尾声了,从初始化到如何添加元素再到copy元素都有所涉及,是时候谈一谈内存的释放了. 是的,对于数据量很小的vector,完全没必要自己进行主动的释放,因为 ...

  9. 实战c++中的vector系列--vector&lt;unique_ptr&lt;&gt;&gt;初始化(全部权转移)

    C++11为我们提供了智能指针,给我们带来了非常多便利的地方. 那么假设把unique_ptr作为vector容器的元素呢? 形式如出一辙:vector<unique_ptr<int> ...

随机推荐

  1. 在启动vsftpd,有时会报错

    在启动vsftpd,有时会报错:C:>ftp 192.168.0.101Connected to 192.168.0.101.220 (vsFTPd 2.0.5)User (192.168.0. ...

  2. Java开源内容管理CMS系统J4CMS集成到JTM

    JTM是Win32下绿色免费的JDK + Tomcat + MySQL环境集成工具. 通过JTM用户无需对JDK.Tomcat.MySQL进行不论什么安装和配置就可以迅速搭建支持JSP + MySQL ...

  3. mysql中char,varchar,text

    1.char char最大长度是255字符,注意是字符数和字符集没关系. 1)可以有默认值, 2)尾部有空格会被截断 3)不管汉字.英文,还是其他编码,都可以存255字符 2.varchar 1)va ...

  4. js:获取节点相关的 nodeName,nodeType,nodeValue

    getElementById() getElementsByName() getElementsByTagName() hasChildNodes() nodeName nodeType=1元素节点/ ...

  5. python接口自动化(二十七)--html 测试报告——上(详解)

    简介 上一篇我们批量执行完用例后,生成的测试报告是文本形式的,不够直观,而且报告一般都是发给leader的,所以最好是直观一目了然,为了更好的展示测试报告,最好是生成 HTML 格式的.unittes ...

  6. logback+slf4j作为日志系统

    一.logback简介 log4j和logback作者是同一人:CekiGülcü.log4j和logback都是实打实的日志系统. commons-logging,slf4j这两者是日志大管家.sl ...

  7. 高效开发 Android App 的 10 个建议

    假如要Google Play上做一个最失败的案例,那最好的秘诀就是界面奇慢无比.耗电.耗内存.接下来就会得到用户的消极评论,最后名声也就臭了.即使你的应用设计精良.创意无限也没用. 耗电或者内存占用等 ...

  8. eclipse生成可执行jar包[转]

    相信大家在开发java的时候一定会遇到要求将java工程打包成可运行的jar的需求,今天我在这篇博客中详细讲解一下生成可运行jar的两种方法,亲测完全可行. 1. 工程中不包含第三方的jar包 这种情 ...

  9. 【Linux】撷取命令cut

    什么是撷取命令啊?说穿了,就是将一段数据经过分析后,取出我们所想要的.或者是经由分析关键词,取得我们所想要的那一行! 不过,要注意的是,一般来说,撷取信息通常是针对『一行一行』来分析的,并不是整篇信息 ...

  10. 【js】js中的||和&&

    逻辑与&&和逻辑或||操作符可以应用于任何类型的操作数,而不仅仅是布尔值. 几乎所有语言中||和&&都遵循“短路”原理, 如&&中第一个表达式为假就不会 ...