类中的 C++ 全局映射,不要打印 map 元素

c++ global map within class, do not print map elements. using cmake

本文关键字:打印 map 元素 映射 C++ 全局      更新时间:2023-10-16

我正在尝试在类 a 中定义一个全局映射,然后在类 b 中使用它。 虽然地图不是空的,但我看不到元素。

另一个困扰我的问题是,我怎么知道我的代码是否使用 C++11 标准编译? 如何设置它,使用 c++98 或 C++2003 编译?

我有 5 个文件:

  1. map_user_a.h

    #include <map>
    #include <string>
    #include <stdint.h>
    using namespace std;
    extern std::map<string, uint8_t> map_global;
    class map_user_a
    {
    public: 
    void init();
    private:
    int user_a;
    };
    
  2. map_user_a.cpp

    #include <iostream>
    #include "map_user_a.h"
    std::map<string, uint8_t> map_global;
    void map_user_a::init()
    {
    std::cout<<"map_user_a::init()"<<endl;
    map_global.insert(std::make_pair("1", 010));
    map_global.insert(std::make_pair("2", 020));
    }
    
  3. map_user_b.h

    #include <map>
    #include <string>
    #include <stdint.h> 
    using namespace std;
    extern std::map<string, uint8_t> map_global;
    class map_user_b
    {
    public:
    void use();
    private:
    int user_b;
    };
    
  4. map_user_b.cpp

    #include <iostream>
    #include "map_user_b.h"
    using namespace std;
    void map_user_b::use()
    {
    cout<<"map_user_b::use()"<<endl;
    cout<<"global map first pair="<< map_global["1"] <<endl;
    }
    
  5. 主.cpp

    #include "map_user_a.h"
    #include "map_user_b.h"
    main()
    {
    map_user_a map_a;
    map_a.init();
    map_user_b map_b;
    map_b.use();
    }
    

现在,我正在使用 cmake 编译 ubuntu 17.10.1 下的代码。

CMakeLists.txt 是

cmake_minimum_required(VERSION 3.9)
project (stl)
add_executable(stl map_user_a.cpp map_user_a.h map_user_b.cpp map_user_b.h main.cpp)

打电话给CMAKE后../build(build是我的构建目录(我正在调用make,代码编译时没有错误。

但是我的输出是第一对空的,尽管地图有元素。

map_user_a::init()
map_user_b::use()
global map first pair=

怎么了? 谢谢。

std::uint8_t

很可能是unsigned char的typedef。 当你向std::ostream提供charunsigned charsigned char时,它不会把它当作一个数字,而是一个字符。 因此,您的程序打印的不是八进制010/十进制8,而是使用该数字作为其代码的字符。 假设您的字符集是 ASCII 的某个扩展名,这是不可打印的字符"退格键",因此您的终端可能只是忽略它。

要将std::uint8_t打印为数字,请将显式强制转换添加到unsigned int

cout << "global map first pair=" << static_cast<unsigned int>(map_global["1"]) << endl;

让我回答你的第二个问题。可以将以下语句添加到 CMakeLists.txt 文件中,以强制编译器使用 C++11 标准

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -std=c++11")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -std=c++11")

其他标准设置如下:

-std=c++
  1. 98,或 -std=c++03 ----- C++98(C++2003(
  2. -std=c++11 ----- C++11
  3. -std=c++14 ----- C++14
  4. -std=c++
  5. 17 或 -std=c++1z ----- C++17
  6. -std=gnu++98 ----- C++98 和 GNU 扩展
  7. -std=gnu++11 ----- C++11 和 GNU 扩展
  8. -std=gnu++14 ----- C++14 和 GNU 扩展
  9. -std=gnu++1z ----- C++17 和 GNU 扩展

至于默认标准,这取决于您使用的编译器和编译器的版本。我认为最好用上面的方法来澄清C++标准。

可以在 CMakeList 中使用以下语句.txt来测试编译器支持的标准并进行设置。

include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++14" COMPILER_SUPPORTS_CXX14)
CHECK_CXX_COMPILER_FLAG("-std=c++17" COMPILER_SUPPORTS_CXX17)
if(COMPILER_SUPPORTS_CXX17)
message(STATUS "support c++17")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17")
elseif(COMPILER_SUPPORTS_CXX14)
message(STATUS "support c++14")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
elseif(COMPILER_SUPPORTS_CXX11)
message(STATUS "support c++11")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
endif()