set map 未完

//#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
#include
using namespace std;
//map
//key 不能重复
template <class T1,class T2>
void prinMap(const map<T1, T2>& m)
{
//map 中的数据是pair
//迭代访问顺序;按照key的中序遍历的顺序
map<T1, T2>::const_iterator it = m.begin();
while (it != m.end())
{
//不能直接输出pair对象
//cout<<*it
//但是是可以通过箭头去访问里边的元素
cout << it->first << “–>” << it->second << endl;
++it;
}
}

//插入
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
#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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值