1.vector
#include <vector>
vector<int> v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
//(1)迭代器遍历方式1
vector<int>::iterator start = v1.begin();//指向容器的初始位置
vector<int>::iterator end = v1.end();//指向元素最后一个位置的后一个位置
while(start != end)
{
cout << *start << endl;
start++;
}
//(2)迭代器遍历方式2
//可以把这里的vector<int>::iterator改成auto,会自动推测
//for(auto start = v1.begin(); start != v1.end(); start++)
for(vector<int>::iterator start = v1.begin(); start != v1.end(); start++)
{
cout << *start << endl;
}
//(3)使用for_each内置算法进行遍历,配合lambda匿名函数
//需要包含头文件#include <algorithm>
for_each(v1.begin(), v1.end(), [](int val)->void { cout << val << endl;});
//(4)使用for_each加函数
template<typename T>
void printer(const T& val)
{
cout << val << endl;
}
for_each(v1.cbegin(), v1.cend(), printer<int>);
//(5)使用for_each加仿函数
template<typename T>
struch functor
{
void operator()(const T& obj)
{
cout << obj << endl;
}
};
for_each(v1.cbegin(), v1.cend(), functor<int>());
//(6)数组下标方式1
int count_1 = v1.size();
for(int i = 0; i < count_1; i++)
{
cout << v1[i] << endl;
}
//(7)数组下标方式2
int count_2 = v1.size();
for(int i = 0; i < count_2; i++)
{
cout << v1.at(i) << endl;
}
//(8)for区间遍历
for(auto val: v1)
{
cout << val << endl;
}
注意:
for(auto a:b)中b为一个容器,效果是利用a遍历并获得b容器中的每一个值,但是a无法影响到b容器中的元素。
for(auto &a:b)中加了引用符号,可以对容器中的内容进行赋值,即可通过对a赋值来做到容器b的内容填充。
2.map
#include <iostream>
#include <map>
using namespace std;
int main() {
map<int, int> _map;
_map[0] = 1;
_map[1] = 2;
_map[10] = 10;
map<int, int>::iterator iter;
iter = _map.begin();
while(iter != _map.end()) {
cout << iter->first << " : " << iter->second << endl;
iter++;
}
// 也可以使用for循环遍历
/*
for(iter = _map.begin(); iter != _map.end(); iter++) {
cout << iter->first << " : " << iter->second << endl;
}
*/
return 0;
}
3.定义数据类型
#include <stdio.h>
#include <vector>
using namespace std;
int main(){
//第一种定义方法
vector<int> v;
for (int i = 0; i < 10; i++){
v.push_back(i);
}
//第二种定义方法
vector<int> v1(v);
//第三种定义方法
vector<int> v2(10, 1);
//第四种定义方法
vector<int> v3(10);
printf("first: ");
for (vector<int>::size_type ix = 0; ix != v.size(); ix ++){
printf("%d\t", v[ix]);
}
printf("\n");
printf("second: ");
for (vector<int>::size_type ix1 = 0; ix1 != v1.size(); ix1 ++){
printf("%d\t", v1[ix1]);
}
printf("\n");
printf("third: ");
for (vector<int>::size_type ix2 = 0; ix2 != v2.size(); ix2 ++){
printf("%d\t", v2[ix2]);
}
printf("\n");
printf("forth: ");
for (vector<int>::size_type ix3 = 0; ix3 != v3.size(); ix3 ++){
printf("%d\t", v3[ix3]);
}
printf("\n");
return 0;
}
4.自测
#include<stdio.h>
#include<map>
#include<iostream>
#include<algorithm>
#include<vector>
#include <unordered_map>
using namespace std;
//两数相加
bool sum(int& a, int b) {
a += b;
cout << "a:" << a << endl;
return true;
}
//遍历vec
bool tarvel_vec(vector<int>& var_vec) {
cout << "开始遍历vector" << endl;
for (const auto& var : var_vec) {
cout << var << "\t" << endl;
}
return true;
}
//遍历map
bool tarvel_map(std::unordered_map<std::string, std::string>& var_map) {//无序map
cout << "开始遍历map" << endl;
for (auto iter = var_map.begin();
iter != var_map.end(); ++iter) {
auto& key = iter->first;
auto& value = iter->second;
cout << "key = "<< key << " value = " << value << endl;
}
return true;
}
int main() {
int a = 10;
int b = 20;
sum(a,b);
cout << a << " a" << endl;
//测试变量c是否为空
string c = "";
if (!c.empty()) {
cout << "c为true,即c不为空" << endl;
} else {
cout << "c为false,即c为空" << endl;
}
//问号运算符,对d进行赋值
int d = a ? a : b;
cout << "d为" << d << endl;
//判断x是够为true
int x = 0;
if (!x) {
cout << "x为false" << endl;
}
//判断自增自减
int f = 10;
int g = f;
--g;
cout << "f: " << f << " g: " << g << endl;
//创建vector,并使用tarvel遍历
vector<int> test_vec;
for (int i = 0; i < 3; ++i) {
test_vec.emplace_back(i);
}
if (!tarvel_vec(test_vec)) {
cout << "travel error by function tarvel_vec" << endl;
}
//定义一个map
std::unordered_map <std::string, std::string > test_map {
{"测试key1","测试value1"},
{"测试key2","测试value2"},
};
test_map.emplace("测试key3", "测试value3");
//遍历map
if (!tarvel_map(test_map)) {
cout << "travel error by function tarvel_map" << endl;
}
return 0;
}
参考https://blog.csdn.net/suguoliang/article/details/88592371