在C++中,经常需要将多个字符串拼接成一个大字符串。这个过程很容易出错,但有一些技巧可以帮助我们轻松地实现这个目标。本文将介绍一些C++中join字符串的技巧。
#include输出结果:#include #include int main() { std::stringstream ss; ss << "Hello, "; ss << "World!"; std::string combined_string = ss.str(); std::cout << combined_string << std::endl; return 0; }
Hello, World!
#include输出结果:#include int main() { std::string s1 = "Hello"; std::string s2 = "World!"; std::string combined_string = s1; for (auto it = s2.begin(); it < s2.end(); it++) { combined_string += *it; } std::cout << combined_string << std::endl; return 0; }
HelloWorld!
#include输出结果:#include int main() { std::string s1 = "Hello"; std::string s2 = "World!"; std::string combined_string = s1 + s2; std::cout << combined_string << std::endl; return 0; }
HelloWorld!
#include输出结果:#include #include #include int main() { std::vector strings = {"Hello ", "World!"}; std::string combined_string = std::accumulate(strings.begin(), strings.end(), std::string("")); std::cout << combined_string << std::endl; return 0; }
HelloWorld!
#include输出结果:#include #include #include int main() { std::vector strings = {"Hello", "World!"}; std::string combined_string = boost::algorithm::join(strings, " "); std::cout << combined_string << std::endl; return 0; }
Hello World!
本文链接:http://task.lmcjl.com/news/28.html