How to concatenate a std::string and an int with C++

2 Answers

0 votes
#include <iostream>

int main() {
    std::string str = "C++";
    int n = 42;

    std::string result = str + std::to_string(n);

    std::cout << result << std::endl;
}


 
/*
run:

C++42

*/

 



answered Jan 24, 2025 by avibootz
0 votes
#include <iostream>
#include <sstream> 

int main() {
    std::string str = "C++";
    int n = 42;

    std::stringstream sstm;
    sstm << str << n;
    std::string result = sstm.str();

    std::cout << result << std::endl;
}


 
/*
run:

C++42

*/

 



answered Jan 24, 2025 by avibootz
...