c++之STL(3) Vector容器
2016-07-21 09:22
537 查看
vector是一个动态数组,是一个类模板(class template)
vector对象的定义和初始化
vcotor对象的操作
vector<T>::size_type
vector下标操作不添加元素
//
vector对象的定义和初始化
vcotor对象的操作
vector<T>::size_type
vector下标操作不添加元素
//
#include<iostream> #include<string> // #include<vector> using std::vector; using std::string; using std::cout; using std::endl; int main() { //using namespace std; // vector<int> ivec; vector<double> dvec; vector<string> svec; vector<int> a; vector<int> b(10, 2); // 存储10 个2 a.push_back(1); a.push_back(2); a.push_back(3); b.push_back(10); b.push_back(11); cout << a.size() << endl; cout << b.size() << endl; //vector容器的循环 for (vector<int>::size_type i = 0; i != a.size(); i++) { cout << a[i] << endl; } for (vector<int>::size_type i = 0; i != b.size(); i++) { cout <<b[i] << endl; } // system("pause"); return 0; }对于容器的循环尽量要用 size_type类型
#include<iostream> #include<string> // #include<vector> using namespace std; int main() { // vector<int> vec1; vec1.push_back(10); vec1.push_back(11); vec1.push_back(12); vector<int> vec2(vec1); //vec1复制到vec2 vector<int> vec4(10, -1); vector<int> vec6(10); // 10个0 vector<string> v5(10, "hello!"); vector<string> v7(10); // 10个空串 cout << vec1[0] << endl; cout << vec2[0] << endl; cout << vec4[0] << endl; cout << vec6[0] << endl; cout << v5[0] << endl; cout << v7[0] << endl; vec1[0] = 100; cout << vec1[0] << endl; v7[0] = "World!"; cout << v7[0] << endl; // system("pause"); return 0; }
#include<iostream> #include<string> // #include<vector> using namespace std; int main() { // vector<int> vec1; cout << vec1.size() << endl; int k; for (vector<int>::size_type ix = 0; ix != 5; ++ix) { cin >> k; vec1.push_back(k); } cout << "显示vector里面的数据:" << endl; for (vector<int>::size_type m = 0; m != vec1.size(); m++) { cout << vec1[m] << endl; } // cout << "请输入一些字符串!" << endl; string word; vector<string> text; while (cin >> word) text.push_back(word); cout << "你输入的字符串是:" << endl; for (vector<string>::size_type i = 0; i != text.size(); i++) { cout << text[i] << endl; } // system("pause"); return 0; }
相关文章推荐
- C语言 malloc realloc calloc
- 嵌入式开发第10天(高级议题,C语言结束)
- C语言setreuid()函数:设置真实及有效的用户识别码
- C++元编程思想(译)
- 基于c++或C的开源搜索引擎
- C语言字符串指针与字符数组的区别
- VC++6.0常用快捷键及对快捷键使用的看法(转)
- C 素数判断&生成素数表
- 基于C++ list中erase与remove函数的使用详解
- C语言strstr函数常用用法
- 简单利用C51的Timer0实现倒计时(F-0)
- C++中的hash_map和map的区别
- 提高项目40-带行号显示文件
- C/C++中声明与定义
- 提高项目39-电子词典
- 提高项目38-带姓名的成绩单
- VC++ 编译libcurl 支持SSL,GZIP
- boost::condition_variable 设计c++ 生产者消费者队列
- 指针变量
- 大话设计模式05----依赖倒转原则 & 里氏代换原则