【C++】常见数据类型转换

int

int2string

string std::to_string (int val);
string std::to_string (long val);
string std::to_string (long long val);
string std::to_string (unsigned val);
string std::to_string (unsigned long val);
string std::to_string (unsigned long long val);
string std::to_string (float val);
string std::to_string (double val);
string std::to_string (long double val);

string

replace

替换 单字符
#include   // replace
#include
#include
 
using namespace std;
int main()
{
    string str="123421657\"abcd\"456789";
 
    cout << str << endl;
    replace(str.begin(),str.end(),'"',' ');
    cout << str << endl;
    return 0;
}
123421657"abcd"456789
123421657 abcd 456789
替换 字符串
#include 
#include 

std::string RepalceAllString(std::string origin_str, const std::string& match_replaced_str, const std::string& new_replace_str) {
    std::string result_str = origin_str;
    for (std::string::size_type pos = 0; pos != std::string::npos; pos += new_replace_str.length()) {
	   // result_str 从下标pos开始查询 match_replaced_str
   	   pos = result_str.find(match_replaced_str, pos);
        if (pos != std::string::npos) {
        	// result_str 下标pos开始 长度为 match_replaced_str.length() 的内容 替换为 new_replace_str
            result_str.replace(pos, match_replaced_str.length(), new_replace_str);
        } else {
            break;
        }
    }
    return result_str;
}

int main()
{
    std::string str = "123421657\"abcd\"456789";

    std::string strreplace = RepalceAllString(str, "\"", "\\\"");

    std::cout << "原始字符串:" << str << std::endl;
    std::cout << "替换后字符串:" << strreplace << std::endl;

    return 0;
}
123421657"abcd"456789
123421657\"abcd\"456789

string2json

C++ 之 C++ 操作 json 文件 - 额外导入jsoncpp 库

#include jsoncpp/json/json.h

rapidjson库的基本使用 - 额外导入rapidjson 库

#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"

【C++】常见数据类型转换_第1张图片
写在最后:若本文章对您有帮助,请点个赞啦 ٩(๑•̀ω•́๑)۶

你可能感兴趣的:(c++,算法,开发语言)