c++容器定义、初始化、赋值
令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 <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 ,d() -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 != d() ; ++i)
{ cout << *i << " ";}
cout << endl;
return ;
}
如果string对象⽤,如下:
string s(10);
编译器报错如下:
<: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 <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 ,d() -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 != d() ; ++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 same
list<string> list2(authors); //right
deque<string> authList(authors); //wrong,type is not same
vector<string> words(articles); //wrong,type is not same
return 0;
}
void print(auto &vec)
{
for(auto i = vec.begin() ; i != d() ; ++i)
{ cout << *i << " ";}
cout << endl;
return ;
数组定义时初始化
}
<: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 same
<: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 same
<: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
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论