stl之map vector

stl之map
https://blog.csdn.net/shuzfan/article/details/53115922

stl之unordered_map
https://blog.csdn.net/hk2291976/article/details/51037095
https://blog.csdn.net/jingyi130705008/article/details/82633778


int main()
{

    //map<string, int> dict;
    //map<string, int>::iterator iter;

    unordered_map<string, int> dict;
    unordered_map<string, int>::iterator iter;

    // 插入数据的三种方式
    dict["apple"] = 1;
    dict.insert(pair<string,int>("banana",2));
    dict.insert(map<string, int>::value_type("orange",3));
    dict["orange"] = 4; // 有键值会修改,没有键值会插入。
    dict.insert(map<string, int>::value_type("orange",5)); // 有键值不操作,没键值会插入。

    cout << dict["lemon"] << endl; // 只要运行[],即便不赋值,也会插入一个默认值0
    cout << dict.count("lemon") << endl; // 输出0
    

    dict.empty() ?
    cout << "no elment " << endl :
    cout << "have " << dict.size() << " element" << endl;


    // 遍历方法
    for(iter = dict.begin(); iter != dict.end(); iter++) {
        cout << iter->first << " " << iter->second << endl;
    }

    for(auto e : dict) {
        cout << e.first <<" " << e.second <<endl;
    }

    // 查找方法
    (iter = dict.find("banana")) != dict.end() ?
    cout << "banana value is " << iter->second <<endl :
    cout << "banana dost not exists" << endl;

    dict.count("watermelon") == 0 ? // 返回键值等于key的元素的个数
    cout << "watermelon dost not exists" << endl:
    cout << "watermelon exists" << endl;

}

vector
https://blog.csdn.net/weixin_33692284/article/details/93203743?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control&dist_request_id=&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control

https://blog.csdn.net/wang13342322203/article/details/94323165

std::vector<int> values (20); // 20个0
std::vector<int> values (20, 1); // 20个1
std::vector<int> values {20}; // 1个20
std::vector<int> values {20, 30}; // 1个20,一个30


std::array<std :: string, 5> words {"one", "two","three", "four", "five"};
std::vector<std::string> words_copy {std::begin(words) , std::end(words)};

返回一维空数组
return vector<int>();
返回二维空数组
vector<<vector<int>> ();

二维数组初始化
vector<vector<int>> vv (len, vector<int> (2, 0));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值