#include <iostream>
#include <vector>
#include <map>
using namespace std;
int main()
{
vector<int> v1;
v1.push_back(100);
v1.push_back(200);
v1.push_back(300);
v1.push_back(400);
v1.push_back(500);
v1.push_back(600);
vector<int> v2(3);
copy(v1.begin(),v1.end(),v2.begin());
for(auto it:v2)
{
cout<< it << endl;
}
string str = "hello, world";
for(auto ch : str)
{
std::cout << ch << " ";
}
cout << endl;
int arr[] = {1, 2, 3, 4};
for(auto i : arr) {
std::cout<< i << std::endl;
}
vector<std::string> str_vec = {"i", "like", "google"};
//通过引用可以修改容器内容
for(auto &it : str_vec) {
it = "c++";
}
for(auto it : str_vec) {
cout << it << " "; // output: c++ c++ c++
}
cout << endl;
map<int, string> hash_map = {{1, "c++"}, {2, "java"}, {3, "python"}};
//遍历map返回的是pair变量,不是迭代器
for(auto it : hash_map)
{
std::cout << it.first << "\t" << it.second << std::endl;
}
return 0;
}
输出为:
100
200
300
h e l l o , w o r l d
1
2
3
4
c++ c++ c++
1 c++
2 java
3 python