C++学习笔记

C++学习笔记,第1张

unordered_map
这个的意思是声明创建的类型是一个普通的string:int的一个umap类型,在这个调用的时候,采用的调用方法类似于pair类型,调用第一个元素就是.first第二个就是.second。
首先从网上找了个代码:
链接为:http://c.biancheng.net/view/7240.html

#include 
#include 
#include 
using namespace std;
int main()
{
    //创建空 umap 容器
    unordered_map umap;
    //构建要添加的键值对
    std::pairmypair("STL教程", "http://c.biancheng.net/stl/");
    //创建接收 insert() 方法返回值的pair类型变量
    std::pair::iterator, bool> ret;
    //调用 insert() 方法的第一种语法格式
    ret = umap.insert(mypair);
    cout << "bool = " << ret.second << endl;
    cout << "iter -> " << ret.first->first <<" " << ret.first->second << endl;
   
    //调用 insert() 方法的第二种语法格式
    ret = umap.insert(std::make_pair("Python教程","http://c.biancheng.net/python/"));
    cout << "bool = " << ret.second << endl;
    cout << "iter -> " << ret.first->first << " " << ret.first->second << endl;
    return 0;
}

由于很久没配置C++了,所以从最开始F5->G++.EXE运行,报错如下:
=thread-group-added,id=“i1”
GNU gdb (GDB) 8.1
Copyright © 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type “show copying”
and “show warranty” for details.
This GDB was configured as “x86_64-w64-mingw32”.
Type “show configuration” for configuration details.
For bug reporting instructions, please see:
http://www.gnu.org/software/gdb/bugs/.
Find the GDB manual and other documentation resources online at:
http://www.gnu.org/software/gdb/documentation/.
For help, type “help”.
Type “apropos word” to search for commands related to “word”.
Warning: Debuggee TargetArchitecture not detected, assuming x86_64.
=cmd-param-changed,param=“pagination”,value=“off”
[New Thread 18932.0x3254]
[New Thread 18932.0x4480]
[New Thread 18932.0x4cc4]
[New Thread 18932.0x40b0]
[Thread 18932.0x4cc4 exited with code 3221225785]
[Thread 18932.0x40b0 exited with code 3221225785]
ERROR: Unable to start debugging. Unexpected GDB output from command “-exec-run”. During startup program exited with code 0xc0000139.
The program ‘d:\project_code\Ccode\map\uordermap.exe’ has exited with code 0 (0x00000000).
参考文章得出要加launch.json等配置文件,具体内容见网址:
https://blog.csdn.net/hrd535523596/article/details/115440598
记得修改一下路径,task.json其实在F5之后就好了,不用再复制上去。

配置好之后还是报错,就离谱。
之后看到是因为launch跟task里面不一样,修改两个的任务名即可。
label跟name改成一样的之后就没问题了,不过按完F5之后还是没有任何输出,让我觉得很奇怪,然后搜索原因:
大部分都说要改啥run in terminal那个扩展设置,不过我那个本来就改了,最后是试了很多,结果发现这个可以:
tasks.json加这个

{
    "tasks": [
      {
        "type": "cppbuild",
        "label": "C/C++: g++.exe build active file",
        "command": "D:/mingw64/bin/g++.exe",
        "args": ["-g", "${file}", "-o", "${fileDirname}\${fileBasenameNoExtension}.exe"],
        "options": {
          "cwd": "${fileDirname}"
        },
        "problemMatcher": ["$gcc"],
        "group": {
          "kind": "build",
          "isDefault": true
        },
        "detail": "compiler: D:/mingw64/bin/g++.exe"
      }
    ],
    "version": "2.0.0"
  }

最后可以了:
输出了下面的一些结果:

bool = 1
iter -> STL教程 http://c.biancheng.net/stl/
bool = 1
iter -> Python教程 http://c.biancheng.net/python/
hello world
Press any key to continue . . . 

这个的话,对应的分别就是最开始复制的那段代码的一些输出,下面对于复制过来的代码进行一一的解释:
unordered_map umap;
这句话是声明一个无序map。
std::pairmypair("STL教程", "http://c.biancheng.net/stl/");
初始化一个pair类型的元素,其中包含了两个字符串类型的元素。
std::pair::iterator, bool> ret;
初始化了一个pair类型的元素,其中包含了一个无序的map跟一个布尔类型的元素。
ret = umap.insert(mypair);
对于无序map进行添加新的元素。
cout << "bool = " << ret.second << endl;
输出出来元素对应的布尔值,并且将第二个元素输出出来。对于pair而言一般第二个元素就是pair.second。
cout << "iter -> " << ret.first->first <<" " << ret.first->second << endl;
输出第一个元素,pair中的,调用pair的元素的时候,第一个第二个对应的基本都是那两个基本定义的时候所设置的那两个元素。
ret = umap.insert(std::make_pair("Python教程","http://c.biancheng.net/python/"));
添加第二个pair的元素,其实这种情况下的话,属于是insert的第二种情况,即添加了一个bool类型的同时添加了一个pair进入原本的umap,对于无序map的 *** 作其实没怎么变,主要变化的地方在于这里面的pair的构造方法,前者是在外面顶一个一个新的pair,后者是类似于并没有赋值,直接就初始化了一个没有变量的一个pair类型的元素。
cout << "bool = " << ret.second << endl;
cout << "iter -> " << ret.first->first << " " << ret.first->second << endl;
return 0;
这几个输出的基本含义同上,不多解释。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/2889481.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-09-14
下一篇 2022-09-14

发表评论

登录后才能评论

评论列表(0条)

保存