C++ 字符串融合 和 string 與 int 之間最簡單的轉換方法

原博文鏈接在我的官方網站,網址是:http://www.aobosir.com/blog/2017/02/07/cpp-string-fusion-and-int-to-string/


字符串融合

#include <string>

std::string str = std::string("../pairAlginClass/capture0001") + std::string(".pcd");
或者
std::string str(std::string("../pairAlginClass/capture0001") + std::string(".pcd"));

string 與 int 之間最簡單的轉換方法

下面的代碼,我們需要使用C++11C++11裏面有std::to_string()方法。

#include <string>

int i = 123;
std::string str = std::to_string(i);

給項目添加C++11

如果是CMakeLists.txt文件的項目。就在CMakeLists.txt文件中添加:

cmake_minimum_required(VERSION 2.8)
set(CMAKE_CXX_STANDARD 11)

如果是Qt項目的,就在.pro文件裏面添加:

CONFIG += c++11

參考網站:

更多精彩的博文,請訪問:http://www.aobosir.com/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章