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。
假设这篇博文对您有帮助,为了好的网络环境,不建议转载,建议收藏!假设您一定要转载。请带上后缀和本文地址。