convert a char to a string in C++

实现方式

参考文档: https://www.techiedelight.com/convert-char-to-string-cpp/

Using std::string constructor

A simple solution is to use the string class fill constructor string (size_t n, char c); which fills the string with n copies of character c.

#include <iostream>
#include <string>
 
int main()
{
    char c = 'A';
 
    // using string class fill constructor
 
    std::string s(1, c);
    std::cout << s << std::endl;
 
    return 0;
}

Using std::stringstream function

Another good alternative is to use a string stream to convert between strings and other numerical types. The idea is to insert the given character into a stream and then write the contents of its buffer to the std::string.

#include <iostream>
#include <string>
#include <sstream>
 
int main()
{
    char c = 'A';
 
    // using stringstream
 
    std::string s;
    std::stringstream ss;
    ss << c;
    ss >> s;                // or, use `s = ss.str()`
    std::cout << s << std::endl;
 
    return 0;
}

Using std::string::push_back function

Another commonly used solution is to use the push_back() function, which is overloaded for chars and appends a character to the string’s end.


#include <iostream>
#include <string>
 
int main()
{
    char c = 'A';
 
    // using string::push_back
 
    std::string s;
    s.push_back(c);
    std::cout << s << std::endl;
 
    return 0;
}

Using std::string::operator+=

string& operator+= (char c);

The string +=operator is overloaded for chars. We can use it to append a character at the end of the string, as shown below:

#include <iostream>
#include <string>
 
int main()
{
    char c = 'A';
 
    // using `std::string::operator+=`
 
    std::string s;
    s += c;
    std::cout << s << std::endl;
 
    return 0;
}

Using std::string::operator=

string& operator= (char c);

Similar to the +=operator, the =operator is also overloaded for chars. We can use it to replace the contents of a string with a single char.

#include <iostream>
#include <string>
 
int main()
{
    char c = 'A';
 
    // using `std::string::operator=`
 
    std::string s;
    s = c;
    std::cout << s << std::endl;
 
    return 0;
}

Using std::string::append function

string& append (size_t n, char c);

We can also use the append() function to append n copies of character c, as demonstrated below:

#include <iostream>
#include <string>
 
int main()
{
    char c = 'A';
 
    // using `std::string::append`
 
    std::string s;
    s.append(1, c);
    std::cout << s << std::endl;
 
    return 0;
}

Using std::string::assign function

string& assign (size_t n, char c);

The assign() function replaces the current value of the string with n copies of character c. We can use it, as shown below:

#include <iostream>
#include <string>
 
int main()
{
    char c = 'A';
 
    // using `std::string::assign`
 
    std::string s;
    s.assign(1, c);
    std::cout << s << std::endl;
 
    return 0;
}

Using std::string::insert function

string& insert (size_t pos, size_t n, char c);

We can use insert() that inserts n copies of character c beginning at character pos.


#include <iostream>
#include <string>
 
int main()
{
    char c = 'A';
 
    // using `std::string::insert`
 
    std::string s;
    s.insert(0, 1, c);
    std::cout << s << std::endl;
 
    return 0;
}

Using std::string::replace function

string& replace (size_t pos, size_t len, size_t n, char c);

The replace() function replaces the portion of the string (len characters beginning at character pos) by n copies of character c. We can use it, as shown below:

#include <iostream>
#include <string>
 
int main()
{
    char c = 'A';
 
    // using `std::string::replace`
 
    std::string s;
    s.replace(0, 1, 1, c);
    std::cout << s << std::endl;
 
    return 0;
}

Converting char to C-string

Finally, we can also convert the char to a C-string and then convert the C-string to a std::string using the fill constructor or std::string::append or std::string::assign.


#include <iostream>
#include <string>
 
int main()
{
    char c = 'A';
 
    // converting char to C-string
    const char* str = &c;
 
    // using `std::string` fill constructor
    std::string s(str, 1);
    std::cout << s << std::endl;
 
    // using `std::string::append`
    std::string S;
    S.append(str, 1);
    std::cout << S << std::endl;
 
    // using `std::string::assign`
    S.assign(str, 1);
    std::cout << S << std::endl;
 
    return 0;
}