//#include
//#include
//using namespace std;
//void TestSet()
//{
// // 用数组array中的元素构造set
// int array[] = { 1, 3, 5, 7, 9, 2, 4, 6, 8, 0, 1, 3, 5, 7, 9, 2, 4, 6, 8, 0 };
// set s(array, array + sizeof(array) / sizeof(array));
// cout << s.size() << endl;
// // 正向打印set中的元素,从打印结果中可以看出:set可去重
// for (auto& e : s)
// cout << e << " ";
// cout << endl;
// // 使用迭代器逆向打印set中的元素
// for (auto it = s.rbegin(); it != s.rend(); ++it)
// cout << *it << " ";
// cout << endl;
// // set中值为3的元素出现了几次
// cout << s.count(3) << endl;
//}
//map set
#include
#include
using namespace std;
void test()
{
set s2;
int array[] = { 1, 3, 5, 7, 9, 2, 4, 6, 8, 0, 1, 3, 5, 7, 9, 2, 4, 6, 8, 0 };
//set 不支持重复,不支持修改
set s2(array, array + sizeof(array) / sizeof(array));
//set迭代器遍历是数据天然有序,本质是迭代器进行的是中序遍历。
set::iterator it = s2.begin();
while (it != s2.end())
{
cout << *it << endl;
++it;
}
cout << endl;
cout << "reverse iterator" << endl;
set<int>::reverse_iterator rit = s2.rbegin();
while (rit != s2.rend())
{
cout << *rit << endl;
++rit;
}
cout << endl;
//插入的操作
//insert(iterator,val);iterator只是一个位置建议,最终数据的位置不一定在指定的位置进行插入
s2.insert(ret.first, 0);
printset(s2);
int array2[] = { 12,9,6,18,39,12,9,6 };
s2.insert(array2, array2 + sizeof(array2) / sizeof(array2))
printset(s2);
//erase删除
int num = s2.erase(39);
printset(s2);
cout << num << endl;
num = s2.erase(399);
cout << num << endl;
s2.erase(s2.begin());
//erase不能删除非法空间
//s2.erase(s2.end());
printset(s2);
s2.erase(++s2.begin(), --s2.end());
printset(s2);
auto it = s2.find(12);
cout << (it != s2.end()) << endl;
it = s2.find(1000);
cout << (it != s2.end()) << endl;
cout << s2.count(12) << endl;
cout << (it != s2.end(1200)) << endl;
}
int main()
{
test();
return 0;
}
#include
//插入
void test1()
{
map<int,int> m;
//初始化的一个情况。
pair<int, int> arr[] = { pair<int,int>(),pair<int,int>(1,2),pair<int,int>(3,3), pair<int,int>(0,0),pair<int,int>(1,3)};
map<int, int> m2(arr,arr+sizeof(arr)/sizeof(arr[0]));
printMap(m2);
}
// accessing mapped values
#include
#include
int main()
{
std::map<char, std::string> mymap;
mymap['a'] = "an element";
mymap['b'] = "another element";
mymap['c'] = mymap['b'];
std::cout << "mymap['a'] is " << mymap['a'] << '\n';
std::cout << "mymap['b'] is " << mymap['b'] << '\n';
std::cout << "mymap['c'] is " << mymap['c'] << '\n';
std::cout << "mymap['d'] is " << mymap['d'] << '\n';
std::cout << "mymap now contains " << mymap.size() << " elements.\n";
return 0;
}