令C表示六个顺序容器类型期中之一(vector,deque,list,forward,string,array),以下详细说明定义和初始化以及赋值.

1.容器定义和初始化

(1)C  c;默认构造函数,如果c是一个array,则c中元素按照默认初始化;否则c为空。

解释:默认初始化,如果c是全局变量,int 初始化为0,如果c是局部变量。那么初始化为任意整数值。string 对象全部初始化为空串子.

(2)C  c1(c2) ;        c1初始化为c2的类型。c1 和c2必须是同类型的constainer.对于array,c1和c2必须长度相同.注意这是初始化。离开定义是不能这样使用的。例如:下面就是错误的,因为初始化时候才能用圆括号。

  vector<int> v_int1 = {1,2,3,4,5,6},v_int2;v_int2(v_int1);

(3)C c1 = c2;     定义c1,并且把c2的赋给c1.包括定义和赋值两个过程。

(4)C c1{a,b,c,d,e,f};把c1列表初始化为花括号内的部分。

(5)C c1 = {a,b,c,d,e,f};把c1列表初始化为花括号内的拷贝。

(6)C c1(b,e);b和e为两个迭代器类型.

(7)C seq(n); seq包含n个元素,这些元素进行了值初始化;此构造函数是explict的。(string不适用).

看下面的c11,d11,l11,f11四个对象。string不适用。

#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <forward_list>
#include <string>
#include <array>
using namespace std;
#define LENGTH  10
typedef  int TYPE;
typedef  vector<TYPE> VECTORTYPE;
typedef  deque<TYPE> DEQUETYPE;
typedef  list<TYPE> LISTTYPE;
typedef  forward_list<TYPE> FORWARD_LISTTYPE;
typedef  array<TYPE,LENGTH>  ARRAYTYPE;
void print(auto &);
ARRAYTYPE a;
int main()
{//test C c(b,e);VECTORTYPE v_type{1,2,3,4,5,6},v_type2;VECTORTYPE v_type1(v_type.begin() + 1 ,v_type.end() -1);VECTORTYPE c11(LENGTH);DEQUETYPE  d11(LENGTH);LISTTYPE   l11(LENGTH);FORWARD_LISTTYPE f11(LENGTH);print(c11);print(d11);print(l11);print(f11); return 0;
}
void print(auto  &vec)
{for(auto i  = vec.begin() ; i != vec.end() ; ++i){ cout << *i << " ";}cout << endl;return ;
}

如果string对象用,如下:

string s(10);

编译器报错如下:

11.cc:30:14: error: invalid conversion from ‘int’ to ‘const char*’ [-fpermissive]string s(10);

(8)C seq(n,t),包含n个t值。array不满足,array<int,10>,有本身的固定类型和长度。

例如:

string s(10,'a');//定义string对象s为10个a
#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <forward_list>
#include <string>
#include <array>
using namespace std;
#define LENGTH  10
typedef  int TYPE;
typedef  vector<TYPE> VECTORTYPE;
typedef  deque<TYPE> DEQUETYPE;
typedef  list<TYPE> LISTTYPE;
typedef  forward_list<TYPE> FORWARD_LISTTYPE;
typedef  array<TYPE,LENGTH>  ARRAYTYPE;void print(auto &);
ARRAYTYPE a;
int main()
{//test C c(b,e);VECTORTYPE v_type{1,2,3,4,5,6},v_type2;VECTORTYPE v_type1(v_type.begin() + 1 ,v_type.end() -1);VECTORTYPE c11(LENGTH,4);DEQUETYPE  d11(LENGTH,5);LISTTYPE   l11(LENGTH,6);FORWARD_LISTTYPE f11(LENGTH,7);print(c11);print(d11);print(l11);print(f11);return 0;
}
void print(auto  &vec)
{for(auto i  = vec.begin() ; i != vec.end() ; ++i){ cout << *i << " ";}cout << endl;return ;
}

note1:标准库array大小:

(1)array<int,10> arr_int{1,2,3,4,5,6,7,8,9,0};

array<int,10>::size_type i;  //right

array<int>::size_type j;//wrong

结论:array的大小是array定义的一部分,array不支持普通的容器构造函数。普通构造函数都会确定容器大小,所以及其容易出错。

  array<int,10> arr_int1{1,2,3,4,5,6,7,8,9,0};array<int,10> arr_int2;array<int,10> arr_int3{1024};cout << endl;  print_array(arr_int1);print_array(arr_int2);print_array(arr_int3);

三个array对象打印如下,说明局部数组的元素的任意随机整数值。

1 2 3 4 5 6 7 8 9 0
0 121 65535 1 -1240874352 32766 993197120 22013 2 0
1024 0 0 0 0 0 0 0 0 0

(2)

  array<int,10> arr_int1{1,2,3,4,5,6,7,8,9,0};array<int,10> arr_int2;array<int,10> arr_int3{1024};array<int,10> arr_int4(arr_int2),arr_int5=arr_int1;//array可以拷贝,或者=赋值

补充1.创建一个容器为另一个容器的拷贝,那么容器类型和元素类型必须匹配。不过,当传递参数是一个迭代器范围时,就不需要容器类型是相同的了。而且,新容器和原容器的元素类型也可以不同,只要能够将要拷贝的元素转换为要初始化的容器的元素类型即可。如下面表1和表2(注意char*可以直接转化成string,反过来必须借助c函数例如c_str(),data成员函数)

          //表1
#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <forward_list>
#include <string>
#include <array>
using namespace std;
#define LENGTH  10
typedef  string TYPE;
typedef  vector<TYPE> VECTORTYPE;
typedef  deque<TYPE> DEQUETYPE;
typedef  list<TYPE> LISTTYPE;
typedef  forward_list<TYPE> FORWARD_LISTTYPE;
typedef  array<TYPE,LENGTH>  ARRAYTYPE;void print(auto &);
ARRAYTYPE a;
int main()
{list<string>  authors = {"Milton","Shakespeare","Austen"};vector<const char*> articles = {"a","an","the"};vector<char*> articles_2(authors); //wrong,type is not samelist<string> list2(authors);    //rightdeque<string> authList(authors); //wrong,type is not samevector<string> words(articles);   //wrong,type is not same return 0;
}
void print(auto  &vec)
{for(auto i  = vec.begin() ; i != vec.end() ; ++i){ cout << *i << " ";}cout << endl;return ;
}11.cc:24:35: error: no matching function for call to ‘std::vector<char*>::vector(std::__cxx11::list<std::__cxx11::basic_string<char> >&)’vector<char*> articles_2(authors); //wrong,type is not same11.cc:26:33: error: no matching function for call to ‘std::deque<std::__cxx11::basic_string<char> >::deque(std::__cxx11::list<std::__cxx11::basic_string<char> >&)’deque<string> authList(authors); //wrong,type is not same11.cc:27:32: error: no matching function for call to ‘std::vector<std::__cxx11::basic_string<char> >::vector(std::vector<const char*>&)’vector<string> words(articles);   //wrong,type is not same
           //表2是 表1修正后的代码
#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <forward_list>
#include <string>
#include <array>
using namespace std;
#define LENGTH  10
typedef  string TYPE;
typedef  vector<TYPE> VECTORTYPE;
typedef  deque<TYPE> DEQUETYPE;
typedef  list<TYPE> LISTTYPE;
typedef  forward_list<TYPE> FORWARD_LISTTYPE;
typedef  array<TYPE,LENGTH>  ARRAYTYPE;void print(auto &);
ARRAYTYPE a;
int main()
{list<string>  authors = {"Milton","Shakespeare","Austen"};vector<const char*> articles = {"a","an","the"};vector<char*> articles_2(authors.begin(),authors.end());  //wrong,string 不能直接转化成char*list<string> list2(authors);    //采用了迭代器初始化,容器类型和元素类型可以不同了。只要元素类型可以转化就okdeque<string> authList(authors.begin(),authors.end());// 同上vector<string>words(articles.begin(),articles.end()); return 0;
}
void print(auto  &vec)
{for(auto i  = vec.begin() ; i != vec.end() ; ++i){ cout << *i << " ";}cout << endl;return ;
}11.cc:24:57:   required from here
/usr/include/c++/7/bits/stl_construct.h:75:7: error: cannot convert ‘std::__cxx11::basic_string<char>’ to ‘char*’ in initialization{ ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }

c++容器定义、初始化、赋值相关推荐

  1. 字符串的定义与赋值及初始化

    字符串的定义与赋值及初始化 C语言的运算符根本无法操作字符串.在C语言中把字符串当作数组来处理,因此,对字符串的限制方式和对数组的一样,特别是,它们都不能用C语言的运算符进行复制和比较操作. 怎么给这 ...

  2. [Spring 深度解析]第7章 IoC容器的初始化过程

    7. IoC容器的初始化过程 ​ 简单来说,IoC容器的初始化是由前面介绍的refresh()方法来启动的,这个方法标志着IoC容器的正式启动.具体来说,这个启动包括BeanDefinition的Re ...

  3. idea中生成spring的 xml配置文件_【132期】面试再被问到Spring容器IOC初始化过程,就拿这篇文章砸他~...

    点击上方"Java面试题精选",关注公众号 面试刷图,查缺补漏 >>号外:往期面试题,10篇为一个单位归置到本公众号菜单栏->面试题,有需要的欢迎翻阅 阶段汇总集 ...

  4. C++ STL容器vector篇(五) vector容器常用初始化操作总结(一维/二维)

    `vector`初始化方法 一维向量 二维向量 参考 vector初始化方法 一维向量 #include <iostream> #include <vector>using n ...

  5. 一步一步手绘Spring IOC运行时序图一(Spring 核心容器 IOC初始化过程)

    相关内容: 架构师系列内容:架构师学习笔记(持续更新) 一步一步手绘Spring IOC运行时序图一(Spring 核心容器 IOC初始化过程) 一步一步手绘Spring IOC运行时序图二(基于XM ...

  6. Spring(二)IOC容器的初始化流程

    文章目录 一.Spring 核心容器类 1.1 BeanFactory 1.2 ApplicationContext 1.3 BeanDefinition 二.IOC容器的初始化 2.1 基于Xml的 ...

  7. 《C++Primer》第九章-顺序容器-学习笔记(1)-顺序容器定义与操作

    <C++Primer>第九章-顺序容器-学习笔记(1) 文章目录 <C++Primer>第九章-顺序容器-学习笔记(1) 摘要 顺序容器的定义 容器元素的初始化 将一个容器初始 ...

  8. 【132期】面试再被问到Spring容器IOC初始化过程,就拿这篇文章砸他~

    程序员的成长之路 互联网/程序员/技术/资料共享 关注 阅读本文大概需要 14 分钟. 作者:拥抱心中的梦想 juejin.im/post/5ab30714f265da237b21fbcc 一.老规矩 ...

  9. Spring容器IOC初始化过程—今天终于进行总结了

    https://www.colabug.com/2539499.html 作为一个经常使用Spring的后端程序员,小编很早就想彻底弄懂整个Spring框架了!但它整体是非常大的,所有继承图非常复杂, ...

最新文章

  1. 大三后端暑期实习面经总结——SSM微服务框架篇
  2. Httpclient学习日记(一)
  3. C++中getline()与cin.getline()详解
  4. Spring 5.0 GA版本发布,支持JDK9及反应式编程
  5. Zephyr应用笔记:mcuboot引导程序简单介绍
  6. lambda表达式语法_使用类似Lambda的语法作为Java中的表达式进行切换
  7. xpath中如何使用变量
  8. weblogic启动脚本
  9. Lync Server 2010下载拓扑报错分析及解决方法分享
  10. Amazon Dynamo论文中文版
  11. CSS 内联样式 外联样式 嵌套样式
  12. PYTHON用时变马尔可夫区制转换(MARKOV REGIME SWITCHING)自回归模型分析经济时间序列...
  13. Python开发指南[1]之程序员计时小时钟(附源码)
  14. 我丈母娘家的小店竟然被Dos攻击了
  15. 申请https证书的过程
  16. Arduino Nano下使用u8glib点亮Oled 128x6
  17. 四川麻将出现天胡与地胡的概率
  18. 小程序用canvas合并两张图片
  19. CV-Model【5】:Transformer
  20. Tomcat源码解析:初始化

热门文章

  1. web.xml 通过contextConfigLocation配置spring 的方式
  2. hibernate数据的三种存在状态(只与事务有关)
  3. Recordset.State 属性
  4. 【python数据挖掘课程】十五.Matplotlib调用imshow()函数绘制热图
  5. 【数据结构与算法】之深入解析“按要求补齐数组”的求解思路与算法示例
  6. Python读取大文件的坑“与内存占用检测
  7. 做人工智能必看的 45 篇论文,附下载地址 | 文末有彩蛋
  8. Jenkins 升级、迁移、备份
  9. 数据库开发——MySQL——pymysql模块
  10. SQL开发技巧 join从句