#include <iostream>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <string>
#include <math.h>
using namespace std;
int vector_test()
{
int n = 20;
vector< int > a(n);
cout << "初始化向量a的大小为:" << a.size() << endl;
vector< int > b(n, 1);
cout << "使用迭代器遍历元素: " << endl;
vector< int >::iterator it = b.begin();
//auto it = b.begin()
for (; it < b.end(); it++) {
cout << " " << *it;
}
cout << endl;
int col = 15, row = 10;
vector< vector < int > > c(row);
cout << "初始化多维向量c的行数大小为:" << c.size() << endl;
vector< vector < int > > d(row, vector <int >(col, 2));
cout << "初始化多维向量c的列数大小为:" << d[0].size() << endl;
auto it1 = d[0].begin();
cout << "使用迭代器遍历第一行的元素: " << endl;
for (; it1 < d[0].end(); it1++) {
cout << " " << *it1;
}
cout << endl;
vector < int > e;
e.assign(b.begin(), b.begin() + 2);
cout << "通过assign将b的0-1的元素赋值给向量e :" << endl;
cout << e[0] << " " << e[1] << endl;
vector <int> f;
f.assign(4, 3);
cout << "通过assign将向量f初始化为4个元素且每个元素值为3: " << endl;
cout << f[0] << " " << f[1] << " " << f[2] << " " << f[3] << endl;
vector <int > g;
g.push_back(1);
g.push_back(2);
g.push_back(3);
cout << "得到第一个元素和第二个元素 : " << endl;
cout << g.front() << " " << g.back() << endl;
cout << "判断向量是否为空:" << endl;
cout << g.empty() << endl;
g.clear();
cout << g.empty() << endl;
g.push_back(1);
g.push_back(2);
g.push_back(3);
g.pop_back();
cout << "删除最后一个元素:" << endl;
cout << g.back() << endl;
g.push_back(3);
g.push_back(4);
g.push_back(5);
cout << g[0] << g[1] << g[2] << endl;
g.erase(g.begin(), g.begin() + 2);
cout << "删除下标0-1的元素后345:" << endl;
cout << g[0] << g[1] << g[2]<< endl;
g.insert(g.begin() + 1, 6);
cout << "在下标 0的元素后面插入6 : 结果为3645" << endl;
cout << g[1] << endl;
int arr[4] = { 1, 2, 3, 4 };
cout << g[0] << g[1] << g[2] << g.size() << endl;
g.insert(g.begin() + 2, arr + 2, arr + 4);
cout << "将数组的第3个元素到第4个元素插入下标为1元素后面: 结果为363445" << endl;
cout << g[2] << g[3] << g[4] << g.size() << endl;
cout << g.capacity() << endl;
g.resize(10, 2);
cout << g.capacity() << " " << g[9] << endl;
sort(g.begin(), g.end());
cout << "将g中的数据进行排序:结果为2222334456" << endl;
cout << g[0] << g[1] << g[2] << g[3] << g[4] << endl;
reverse(g.begin(), g.end());
cout << g[0] << g[1] << g[2] << g[3] << g[4] << endl;
cout << g[5] << g[6] << g[7] << g[8] << g[9] << endl;
copy(b.begin(), b.begin() +5, g.begin());
cout << g[0] << g[1] << g[2] << g[3] << g[4] << endl;
cout << g[5] << g[6] << g[7] << g[8] << g[9] << endl;
auto post = find(g.begin(), g.end(), 3);
if (post != g.end()) {
cout << *post << endl;
}
return 0;
}
int QueueTest()
{
queue < int > q1;
q1.push(1);
q1.push(2);
cout << "队列个数结果2:" << endl;
cout << q1.size() << endl;
cout << "返回最第一个元素结果1:" << endl;
cout << q1.front() << endl;
cout << "返回最后一个元素结果2:" << endl;
cout << q1.back() << endl;
q1.pop();
q1.pop();
cout << "判断队列是否为空结果1:" << endl;
cout << q1.empty() << endl;
return 0;
}
void StackTest()
{
stack < int > sta;
sta.push(1);
sta.push(2);
cout << "栈个数结果2:" << endl;
cout << sta.size() << endl;
cout << "返回最第一个元素结果2:" << endl;
cout << sta.top() << endl;
sta.pop();
cout << "返回最第一个元素结果1:" << endl;
cout << sta.top() << endl;
sta.pop();
cout << "判断栈是否为空结果1:" << endl;
cout << sta.empty() << endl;
}
void StringTest()
{
string str = "abcdefghijklmncde";
string::size_type position;
auto last_pos = str.find_last_of("cde");
position = str.find("cde");
if (position != str.npos) {
cout << position << endl;
}
position = str.find("cde", 5);
if (position != str.npos) {
cout << position << endl;
}
if (last_pos != str.npos) {
cout << last_pos << endl;
}
}
void PairTest()
{
pair<string, int> pa;
pa = make_pair("abc", 1);
cout << pa.first << " " << pa.second << endl;
pair< int, int >pa1(1, 2);
cout << pa1.first << " " << pa1.second << endl;
}
void MapTest()
{
unordered_map<int, string> ma;
ma.insert(pair<int, string>(1, "abc"));
cout << "1 " << ma[1] << endl;
ma.insert(map<int, string>::value_type(4, "test"));
cout << "4 " << ma[4] << endl;
ma.insert(make_pair(2, "bcd"));
cout << "2 " << ma[2] << endl;
ma[1] = "first";
ma[3] = "def";
cout << "3 " << ma[3] << endl;
for (auto it = ma.begin(); it != ma.end(); it++) {
cout << "map的大小为: " << ma.size() << it->first << " " << it->second << endl;
}
cout << "判断是否存在关键字1 1:" << ma.count(1) << endl;
auto it1 = ma.begin();
it1 = ma.find(2);
cout << it1->first << it1->second << endl;
ma.erase(2);
cout << "大小变为3:" << ma.size() << endl;
ma.erase(ma.begin(), ma.end());
cout << "判断是否为空1:" << ma.empty() << endl;
ma[1] = "alksdjf";
cout << "判断是否为空0:" << ma.empty() << endl;
ma.clear();
cout << "判断是否为空1:" << ma.empty() << endl;
}
int main()
{
vector_test();
QueueTest();
StackTest();
StringTest();
PairTest();
MapTest();
return 0;
}