推荐使用string

时间:2023-03-08 16:25:51

C-string(char* const char*)

basic_string<>
特化版本:
string char
wstring wchar_t
u16string char16_t
u32string char32_t

Setlocale是一个计算机函数,功能是用来配置地域的信息,设置当前程序使用的本地化信息。
若 locale 为零(NULL),则不会改变地域化配置,返回当前的地域值,若系统尚未实作则返回 false。
使用系统默认的设置调用setlocale(LC_ALL,"");

wcout 输出 wstring需设置setlocale(LC_ALL, "chs");或wcout.imbue(locale("chs"));

wstring ws;
//wcout.imbue(locale("chs"));
setlocale(LC_ALL, "chs");

while (getline(wcin, ws))
{
for (int i = ws.length() - 1; i >= 0; --i)
{
wcout << ws[i];
}
cout << endl;
}

setlocale(LC_ALL, "C");

= , assign() 赋予新值
swap() 交换
+=, append(),push_back() 添加字符
erase(), pop_back() 删除字符
clear() 移除全部字符
resize() 改变字符数量(在尾段删除或添加字符)
replace() 替换字符
empty() 判断是否为空
size(),length() 返回字符数量
max_size() 返回字符的最大可能个数
capacity() 返回重分配前的字符容量
reserve() 保留一定量内存以容纳一定数量的字符
shrink_to_fit() 缩减内存使符合当前的字符量
[],at() 某一个字符
front(),back() 第一个、最后一个字符
>>,getline() 从一个stream读取某值
<< 将某值写入stream

copy() 将string复制到一个字符array
data(),c_str() 将string以C-string形式返回,以'\0'结尾,就是其字符串的指针
substr() 子字符串
find
begin(),end() 迭代器

stoi() stol() stoll() stoul() stoull()
stof() stod() stold()
to_string(),to_wstring()

string s("nico");//s.length() 4
cout << s << "123\n";

string s1("nico", 5);//s.length() 5
cout << s1 << "123\n";

string s2(5,'\0'); //s.length() 5
cout << s2 << "123\n";

//string s3('x'); //ERROR
string s3(1, 'x');
string s4({'x'});

'\0' 在string中不具特殊意义,但在一般C-string中却用来标识结束
不要以NULL取代nullptr或取代char*参数,因为NULL具有整数类型

data(),c_str()的返回值得有效期限在下次调用non-const成员函数时即告终止

string s("hello");
foo(s.c_str());

const char* p;
p = s.c_str();
foo(p);

s += "ext";
foo(p);//helloext

string没有format
采用

包含头文件#include <sstream>

stringstream ss;
ss << i << "dddd" << 3 << "4444";
string s ;
ss >> s; 或 s = ss.str();//推荐用后者

每次用完用ss.str("");来清空内容,否则下次再用的话会和上次的串连在一起

或使用sprintf_s

std::string string_format(const char* format, ...)
{
#if 1 // 最大长度限制:1024 - 1
char buff[1024] = { 0 };

int _Result;
va_list args;
__crt_va_start(args, format);
_Result = vsprintf_s(buff, sizeof(buff), format, args);
__crt_va_end(args);

std::string str(buff);
return str;
#else // 无长度限制
va_list args;
va_start(args, format);
int count = vsnprintf(NULL, 0, format, args); // 使用vsnprintf:warning C4996; 使用vsnprintf_s:无法自动计算长度
va_end(args);

va_start(args, format);
char* buff = (char*)malloc(count * sizeof(wchar_t));
vsnprintf(buff, count, format, args);
va_end(args);

std::string str(buff, count);
free(buff);
return str;
#endif
}

string与wstring之间的转换:

一、使用CRT库的mbstowcs()函数和wcstombs()函数,平台无关,需设定locale。

#include <string>
#include <locale.h> std::wstring s2ws(const std::string& s){
std::string curLocale = setlocale(LC_ALL, NULL); //curLocale="C"
setlocale(LC_ALL, "chs");
const char* _Source = s.c_str();
size_t _Dsize = sizeof(char)*s.size() + 1;
wchar_t* _Dest = new wchar_t[_Dsize];
wmemset(_Dest, 0, _Dsize);
size_t convertedChars = 0;
mbstowcs_s(&convertedChars, _Dest, _Dsize, _Source, _TRUNCATE);
//mbstowcs(_Dest, _Source, charNum);
std::wstring result = _Dest;
delete[] _Dest;
setlocale(LC_ALL, curLocale.c_str());
return result;
} std::string ws2s(const std::wstring& ws)
{
std::string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C"; setlocale(LC_ALL, "chs"); const wchar_t* _Source = ws.c_str();
size_t _Dsize = 2 * ws.size() + 1;
char *_Dest = new char[_Dsize];
memset(_Dest, 0, _Dsize);
size_t convertedChars = 0;
wcstombs_s(&convertedChars, _Dest, _Dsize, _Source, _TRUNCATE);
//wcstombs(_Dest, _Source, _Dsize);
std::string result = _Dest;
delete[]_Dest; setlocale(LC_ALL, curLocale.c_str()); return result;
} 2.采用ATL封装_bstr_t的过渡:(注,_bstr_是Microsoft Specific的,无移植性) #include <string>
#include <comutil.h>
using namespace std;
#pragma comment(lib, "comsuppw.lib") string ws2s(const wstring& ws);
wstring s2ws(const string& s); string ws2s(const wstring& ws)
{
_bstr_t t = ws.c_str();
char* pchar = (char*)t;
string result = pchar;
return result;
} wstring s2ws(const string& s)
{
_bstr_t t = s.c_str();
wchar_t* pwchar = (wchar_t*)t;
wstring result = pwchar;
return result;
} 3.调用Windows API
#include <string>
#include <windows.h>
using namespace std; std::string WChar2Ansi(LPCWSTR pwszSrc)
{
int nLen = WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, NULL, 0, NULL, NULL); if (nLen<= 0) return std::string(""); char* pszDst = new char[nLen];
if (NULL == pszDst) return std::string(""); WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, pszDst, nLen, NULL, NULL);
pszDst[nLen -1] = 0; std::string strTemp(pszDst);
delete [] pszDst; return strTemp;
} string ws2s(wstring& inputws){ return WChar2Ansi(inputws.c_str()); } //Converting a Ansi string to WChar string std::wstring Ansi2WChar(LPCSTR pszSrc, int nLen) {
int nSize = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pszSrc, nLen, 0, 0);
if(nSize <= 0) return NULL; WCHAR *pwszDst = new WCHAR[nSize+1];
if( NULL == pwszDst) return NULL; MultiByteToWideChar(CP_ACP, 0,(LPCSTR)pszSrc, nLen, pwszDst, nSize);
pwszDst[nSize] = 0; if( pwszDst[0] == 0xFEFF) // skip Oxfeff
for(int i = 0; i < nSize; i ++)
pwszDst[i] = pwszDst[i+1]; wstring wcharString(pwszDst);
delete pwszDst; return wcharString;
} std::wstring s2ws(const string& s){ return Ansi2WChar(s.c_str(),s.size());}