C++中數組非常坑,有沒有相似Python中list的數據類型呢?相似的就是vector!vector 是同一種類型的對象的集合,每一個對象都有一個對應的整數索引值。
和 string 對象一樣。標准庫將負責管理與存儲元素相關的內存。我們把 vector 稱為容器,是由於它能夠包括其它對象。
一個容器中的全部對象都必須是同一種類型的。
vector對象的定義和初始化
vector<T> v1; | 保存類型為 T 對象。默認構造函數 v1 為空。 |
vector<T> v2(v1); | v2 是 v1 的一個副本。 |
vector<T> v3(n, i); | v3 包括 n 個值為 i 的元素。 |
vector<T> v4(n); | v4 含有值初始化的元素的 n 個副本。 |
個數,元素值指定每一個元素的初始值】
vector對象動態增長:
vector 對象(以及其它標准庫容器對象)的重要屬性就在於能夠在執行時高效地加入元素。
【注意:由於 vector 增長的效率高,在元素值已知的情況下,最好是動態地加入元素。】
值初始化:
/*
* vector_test.cpp
*
* Created on: 2014年6月24日
* Author: The_Third_Wave
*/
#include <iostream>
#include <string>
#include <vector>
using std::string; using std::vector; using std::cout; using std::endl;
void pr_int_vector(vector<int> vec)
{
for(auto &v : vec)
{
cout<<v<<" ";
}
cout<<endl;
}
void pr_str_vector(vector<string> vec)
{
for(auto &v : vec)
{
cout<<v<<" ";
}
cout<<endl;
}
int main()
{
vector<int> a;
vector<int> b(a);
vector<int> c(10, 23);
vector<string> s1(10, "null");
vector<string> s2(10);
vector<string> s3 = {10, "hi!"}; // 重點關注
vector<string> s4 = {"10", "hi!"}; // 重點關注
pr_int_vector(a);
pr_int_vector(b);
pr_int_vector(c);
pr_str_vector(s1);
pr_str_vector(s2);
pr_str_vector(s3);
pr_str_vector(s4);
return 0;
}
23 23 23 23 23 23 23 23 23 23
null null null null null null null null null null
hi! hi! hi! hi! hi! hi! hi! hi! hi! hi!
10 hi!
【更新於2014.06.25】能夠利用函數重載。僅僅有1個函數名(臨時和Python還是有區別的,為什么不能僅僅定義一個函數呢?自己主動識別參數。興許待求證)
void pr_vector(const vector<int> &vec)
{
// 由於是輸出而不是改動。定義形參為常量引用,提高可靠性和效率!
for(auto &v : vec)
{
cout<<v<<" ";
}
cout<<endl;
}
void pr_vector(const vector<string> &vec)
{
// 由於是輸出而不是改動,定義形參為常量引用。提高可靠性和效率!
for(auto &v : vec)
{
cout<<v<<" ";
}
cout<<endl;
}
至於為什么range for 語句里使用的還是引用(&),保持好習慣!
並且,這樣的情況真的去改動值,會報錯,由於是const & 類型,函數中不能改變實參!
vector對象操作方法
和string相似!v.empty()
v.size()
2、使用 size_type 類型時,必須指出該類型是在哪里定義的。vector 類型總是包括總是
包括 vector 的元素類型vector<int>::size_type】
v.push_back(t)
下面為樣例:
#include <iostream>
#include <string>
#include <cctype>
#include <vector>
int main()
{
// read words from the standard input and store them as elements in a vector
std::string word;
std::vector<std::string> text; // empty vector
while (std::cin >> word)
{
text.push_back(word); // append word to text
for(std::vector<int>::size_type ix =0; ix != text.size(); ++ix)
std::cout<<"Now text["<<ix<< "]is: "<<text[ix]<<std::endl;
}
return 0;
}
結果為:
Hello
Now text[0]is: Hello
world!
Now text[0]is: Hello
Now text[1]is: world!
注意:
和Python區別太大了。。
。
理所當然,緩沖區溢出了,黑客們太喜歡了。】
有人會操心效率?別操心!代價非常小【內聯函數【更新於2014.06.25:內聯函數,在函數返回類型前面用關鍵詞inline定義----目的是把函數內聯到主程序里面。相當於嵌入。作用就是省略了保存當前位置,調到函數位置執行再跳轉的過程。】】。
v[n]
v1 = v2
v1 == v2
!=, <, <=,>, and >=
Have their normal meanings保持這些操作符慣有的含義。一個簡單的樣例
讀入一段文本到 vector 對象,每一個單詞存儲為 vector 中的一個元素。
把vector 對象中每一個單詞轉化為大寫字母。
輸出 vector 對象中轉化后的元素,每八個單詞為一行輸出。
假設文本為:in the vector. transform each word into uppercase letters. Print the transformed elements from the vector, printing eight words to a line.
【2014.06.24備注:使用c++11新特性的改寫樣例在C/C++中字符串String及字符操作方法(http://blog.csdn.net/zhanh1218/article/details/33306481)一文中】
#include <iostream>
#include <string>
#include <vector>
std::string deal_word(std::string word)
{
std::string WORD; // 創建空字符串
for(std::string::size_type ix =0; ix != word.size(); ++ix)
{
if (not ispunct(word[ix]))
{
WORD += toupper(word[ix]); //連接非標點字符到字符串
}
}
return WORD;
}
int main()
{
std::string word; // 緩存輸入的單詞
std::vector<std::string> text; // empty vector
std::cout<<"Please input the text:"<<std::endl; //提示輸入
while (std::cin >> word and word != "INPUTOVER") // INPUTOVER 用於標示輸入結束。也能夠ctrl + z停止輸入
{
word = deal_word(word); // 單詞處理
text.push_back(word); // append word to text
}
for(std::vector<int>::size_type ix =0, j = 0; ix != text.size(); ++ix, ++j)
{
if (j==8) // 8個單詞一行
{
std::cout<<std::endl; //換行
j = 0; //又一次計數
}
std::cout<<text[ix]<<" "; //加空格!
}
return 0;
}
結果為:
Please input the text:
in the vector. transform each word into uppercase letters. Print the transformed elements from the vector, printing eight words to a line. INPUTOVER
IN THE VECTOR TRANSFORM EACH WORD INTO UPPERCASE
LETTERS PRINT THE TRANSFORMED ELEMENTS FROM THE VECTOR
PRINTING EIGHT WORDS TO A LINE
本文由@The_Third_Wave(Blog地址:http://blog.csdn.net/zhanh1218)原創。由於還有部分內容沒有接觸,僅僅講了大概沒有原因。會不定期更新,有錯誤請指正。
假設你看到這篇博文時發現不完整。那是我為防止爬蟲先公布一半的原因,請看原作者Blog。
假設這篇博文對您有幫助,為了好的網絡環境,不建議轉載,建議收藏!假設您一定要轉載。請帶上后綴和本文地址。