C++刷题入门级库函数

说明:本帖长期更新,记录刷题中自己对C++知识储备的不足

 

一 . 数据结构j基础操作:

1.vector操作

vector<int> vec;
vector<vector<int>> result;  //定义一个二维数组



vec.push_back(num)    //在vec最后插入一个元素
vec.pop_back()        //在vec移除最后一个元素
vec.clear()           //清空所有元素

sort(vec.begin(),vec.end());


//erase示例:
int main ()
{
  std::string str ("This is an example sentence.");
  std::cout << str << '\n';
                                           // "This is an example sentence."
  str.erase (10,8);                        //            ^^^^^^^^
  std::cout << str << '\n';
                                           // "This is an sentence."
  str.erase (str.begin()+9);               //           ^
  std::cout << str << '\n';
                                           // "This is a sentence."
  str.erase (str.begin()+5, str.end()-9);  //       ^^^^^
  std::cout << str << '\n';
                                           // "This sentence."
  return 0;
}

2.unordered_map

#include<unordered_map>

void twoSum(vector<int> &nums)
{
    unordered_map<int, int> mapping; //定义一个map
    for (int i = 0; i < nums.size(); i++)
    {
        mapping[nums[i]] = i;
    }
}


//引自https://www.cnblogs.com/langyao/p/8823092.html
int main()
{
    unordered_map<string, double>
        myrecipe,
        mypantry = {{"milk", 2.0}, {"flour", 1.5}};

    /****************插入*****************/
    pair<string, double> myshopping("baking powder", 0.3);
    myrecipe.insert(myshopping);                             // 复制插入
    myrecipe.insert(make_pair<string, double>("eggs", 6.0)); // 移动插入
    myrecipe.insert(mypantry.begin(), mypantry.end());       // 范围插入
    myrecipe.insert({{"sugar", 0.8}, {"salt", 0.1}});        // 初始化数组插入(可以用二维一次插入多个元素,也可以用一维插入一个元素)
    myrecipe["coffee"] = 10.0;                               //数组形式插入

    display(myrecipe, "myrecipe contains:");

    /****************查找*****************/
    unordered_map<string, double>::const_iterator got = myrecipe.find("coffee");

    if (got == myrecipe.end())
        cout << "not found";
    else
        cout << "found " << got->first << " is " << got->second << "\n\n";
    /****************修改*****************/
    myrecipe.at("coffee") = 9.0;
    myrecipe["milk"] = 3.0;
    display(myrecipe, "After modify myrecipe contains:");

    /****************擦除*****************/
    myrecipe.erase(myrecipe.begin()); //通过位置
    myrecipe.erase("milk");           //通过key
    display(myrecipe, "After erase myrecipe contains:");

    /****************交换*****************/
    myrecipe.swap(mypantry);
    display(myrecipe, "After swap with mypantry, myrecipe contains:");

    /****************清空*****************/
    myrecipe.clear();
    display(myrecipe, "After clear, myrecipe contains:");
    return 0;
}

 

二.排序

sort(nums.begin(),nums.end(),std::less<int>()); // 从小到大
sort(nums.begin(),nums.end(),std::greater<int>()); //从大到小

is_sorted(nums.begin(),nums.end(),std::less<int>()); // 从小到大
is_sorted(nums.begin(),nums.end(),std::greater<int>()); // 从大到小

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值