OOIII/

C++ 转换char到 std::string

介绍几种char转string的方式:

1. std::string constructor
string(size_t n, char c),使用n个字符c初始化string对象。

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

2、std::stringstream
将给定字符串插入到stringstream中,然后将其缓冲区内容写入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 s = ss.str();
    std::cout << s << '\n';
 
    return 0;
}

3、std::string::push_back
使用push_back()添加字符到string的尾部

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

4、std::string::operator+=
string& operator+=(char c) 利用重载操作符添加字符c到string尾部

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

5、std::string::operator=
类似operator+=重载操作符,直接替换string内容

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

6、std::string::apend
string& append(size_t n,char c) 追加n个字符c到string尾部

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

7、std::string::assing
string& assign(size_t n,char c)用n个字符c来替换当前的string内容

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

8、std::string::insert
string& insert(size_t pos,size_t n,char c)从string的pos开始,插入n个字符c

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

9、std::string::replace
string& replace(size_t pos,size_t len,sie_t n,char c)用n个字符c替换string中从pos开始的len个字符

#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 << '\n';
 
    return 0;
}

10、把字符串转换为const char* 然后再转为string(通过constructor、append或者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 << '\n';
 
    // using std::string::append
    std::string S;
    S.append(str, 1);
    std::cout << S << '\n';
 
    // using std::string::assign
    S.assign(str, 1);
    std::cout << S << '\n';
 
    return 0;
}

翻译自 https://www.techiedelight.com/convert-char-to-string-cpp/

留下一条评论

暂无评论