0
点赞
收藏
分享

微信扫一扫

量化交易之C++篇 - map 容器

witmy 2023-03-02 阅读 62


#include <iostream>
#include <map>

using namespace std;


void test01() {

map<int, int> map1;
// 插入值的 四种方式
map1.insert(pair<int, int>(1, 10));
map1.insert(make_pair(2, 20));
map1.insert(map<int, int>::value_type(3, 30));
map1[4] = 40;

// 遍历
for (map<int, int>::iterator it = map1.begin(); it != map1.end(); it++) {
cout << "key: " << it->first << " value: " << it->second << endl;
}
}


void test02() {
map<int, int> map1;
// 插入值的 四种方式
map1.insert(pair<int, int>(1, 10));
map1.insert(make_pair(2, 20));
map1.insert(map<int, int>::value_type(3, 30));
map1[4] = 40;

// 删除 1 这一对
map1.erase(1);
for (map<int, int>::iterator it = map1.begin(); it != map1.end(); it++) {
cout << "key: " << it->first << " value: " << it->second << endl;
}

map<int, int>::iterator position = map1.find(1);
if (position != map1.end()) {
cout << "找到 key: " << position->first << "value: " << position->second << endl;
} else {
cout << "未找到" << endl;
}

int number = map1.count(3);
cout << "number:" << number << endl; // 1: 找到 2: 没找到

map<int, int>::iterator result = map1.lower_bound(3);
if (result != map1.end()) {
cout << "lower_bound key: " << result->first << "value: " << result->second << endl;
}
}


int main() {

//test01();

test02();

return EXIT_SUCCESS;
}

 

举报

相关推荐

0 条评论