日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

c++容器定义、初始化、赋值

發布時間:2024/4/18 c/c++ 55 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 <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)...); }

?

?

?

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的c++容器定义、初始化、赋值的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。