类中的 C++ 全局映射,不要打印 map 元素
c++ global map within class, do not print map elements. using cmake
我正在尝试在类 a 中定义一个全局映射,然后在类 b 中使用它。 虽然地图不是空的,但我看不到元素。
另一个困扰我的问题是,我怎么知道我的代码是否使用 C++11 标准编译? 如何设置它,使用 c++98 或 C++2003 编译?
我有 5 个文件:
-
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; };
-
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)); }
-
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; };
-
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; }
-
主.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
提供char
、unsigned char
或signed 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++- 98,或 -std=c++03 ----- C++98(C++2003(
- -std=c++11 ----- C++11
- -std=c++14 ----- C++14 -std=c++
- 17 或 -std=c++1z ----- C++17
- -std=gnu++98 ----- C++98 和 GNU 扩展
- -std=gnu++11 ----- C++11 和 GNU 扩展
- -std=gnu++14 ----- C++14 和 GNU 扩展
- -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()
- 如何循环打印顶点结构
- 为什么在popback()操作之后,它仍然打印完整的矢量
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 类中的 C++ 全局映射,不要打印 map 元素
- 使用 std::for_each 迭代和打印 std::map
- 有条件的打印和计数在 std::map 上有限制
- 如何在C++中打印矢量<map<string,int> >
- C++ Map erase(),迭代器打印 erased 元素
- 为什么在打印Un_ordered Map and Map(字典)的密钥和值时存在异常
- 打印 Map 的元素值时出错
- C++ 如何打印此地图 std::map<int, pair<vector<pair<int, int>>, int>>
- 将结果存储在 C++Map 中,然后迭代它,然后打印出来
- 如何将结果存储在 Map 中 C++,然后迭代它,然后打印出结果
- C++:使用 map 对文本文件中出现的字符串值进行排序和打印
- 在std::map中打印迭代器的索引
- 用一个函数打印map和unordered_map对
- std::map<string, class> 打印键的值
- 如何打印这种类型map的值map
, int>关联[2] - 如何打印 map<string、map<int、int>> to cout 的内容?