#include "stdio.h"
#include "iostream"
#include "string"
#include "locale.h"
//#include "winapi.h"
using namespace std;
/*
编码统一化,编写单一源代码
如果我们建立的两个版本的程序,一个处理ACSII字符集,另一个处理Unicode字符串,
最好的解决方法是编写出一个既能按照ASCII编译
又能按照Unicode编译的单一源代码,主要核心的利用宏定义,进行条件编译
#ifdef_UNICODE
typedef wstring tstring;
typedef wchar_t tchar;
#define _T(x) L## X
#else
typedef string tstring;
typydef char tchar;
#define _T(x) x
#endif
*/
#ifdef _UNICODE
typedef wstring tstring;
typedef wchar_t tchar;
#define _T(x) L## X
#else
typedef string tstring;
typedef char tchar;
#define _T(x) x
#endif
//string --> wstring
wstring s2ws(const string &s)
{
string curLocale = setlocale(LC_ALL, NULL);//curLocale = "C"
setlocale(LC_ALL, "chs");
const char * _Source = s.c_str();
size_t _Dsize = s.size() + 1;
wchar_t * _Dest = new wchar_t[_Dsize];
wmemset(_Dest, 0, _Dsize);
mbstowcs(_Dest, _Source, _Dsize);
wstring result = _Dest;
delete[] _Dest;
setlocale(LC_ALL, curLocale.c_str());
return result;
}
//wstring --> string
string ws2s(const wstring &ws)
{
string curLocale = setlocale(LC_ALL, NULL);
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);
wcstombs(_Dest, _Source, _Dsize);
string result = _Dest;
delete[] _Dest;
setlocale(LC_ALL, curLocale.c_str());
return result;
}
int main()
{
try
{
//string 构造函数,利用好构造函数更高效,每一个对象都需要构造,在构造的时候赋初值
//string() string(const string & s) string(const char *s) string(const int n, const char c)
int sib = 0;
string a;//string()
string b(a);//string(const string & s)
string c("aaa");//string(const char *s)
string d(3, 'g');// string(const int n, const char c)
//string的赋值
string s1 = "test stl";//string &operator=(const char * s);
cout << "s1" << s1.c_str();
string s2 = s1;//string &operator=(const string &s);
cout << "s2" << s2.c_str() << endl;
string s3;//string()
s3.assign("abcd");//string & assign(const char *s);
cout <<"s3" << s3.c_str() << endl;
char c1[10] = {'1','2','3','4','5','6'};
string s4;
s4.assign(c1, 4);
cout << "c1" << c1 << ", s4:" << s4.c_str() << endl;
//int length() const;//返回当前字符串的长度,长度不包括字符串结尾的'\0'
//bool empty() const;//判断当前字符串是否为空
//string 存取字符操作
//const char & operator[](int n) const;//不能被try
//const char & at(int n) const; //可以被try,程序健壮性强
//char &operator[] (int n);
//char &at(int n);
//string的成员函数
//int copy(char * s, const int n, const int pos);//从string中this获取从下标为pos开始的n个字符,复制到s指针指向的空间中(保证s的空间足够大) ‘、0’
string str1 = "1234567890";
char cstr1[6];//默认没有赋初值,内存中数据未知
memset(cstr1, 0, sizeof(cstr1));//
int clen = strlen(cstr1);//
int csizeof = sizeof(cstr1);//计算数组长度要用sizeof
int nCount = str1.copy(cstr1, 2, 2);
int nCount = str1._Copy_s(cstr1, strlen(cstr1), 2, 3);//copy这个函数并没有把'\0'拷贝过来
//cstr1[csizeof - 1] = '\0';
//clen = strlen(cstr1);
//csizeof = sizeof(cstr1);
//char cstr2[5] = "1234";
//string find 正向查找,第一个匹配的字符的下标
//int find(const char c, int pos = 0) const;
//int find(cosnt char *s, int pos = 0) cosnt;
//int find(const string & s, int pos = 0) const;
//string rfind 反向查找
//int rfind(const char c, const int pos = 0) const;
//int rfind(const char *s,
//string的连接
//string & operator+(const string &s);
string strA = "hello ";
string strB = "world";
string strC = strA + strB;
cout << "strC:" << strC << "strC.Len :" << strC.length() << endl;
//string & operator+=(const string &s);
//string & operator+(const char *s);
//string & operator+=(const char *s);
//string & append(const string &s);
//string & append(const char *s);
//string & append(const char *s, const int n);
//string & append(const string & s, const int n, const int pos);
//string & append(const int n, char c);
string strD;
strD.append(strA);
//string 比较,> 的时候返回1, =的时候返回0,<的时候返回-1,比较的时候参考字典顺序,前面的小,后面的大,A < a
//int compare(const string &s) const;//string 中的this不能变,最后要加const, 输入的s不能变,加const, 不能改变的都要加const,加引用
//int compare(cosnt char * s) const;//
//string 的截取
//string & substr(const int start, const int end) const ;
//string 插入 insert
//string &insert(int pos, const char *s);
//string &insert(int pos, const string &s);
//string &insert(int pos, int n, char c);
//string 替代 replace
//string earse 删除
//string &earse(int pos, int n);
strA.erase(1,2);
//string swap 交换
//string clear
//string 是对char* 的管理,wstring是对wchar的管理
//本质为char和wchar的区别,char占用一个字节,wchar 占用两个字节,wstring的函数和string的一样的
wstring wstrA;
wstrA = s2ws("我爱中国");
//wstring wstrA(L"我爱中国");
wcout.imbue(locale("chs"));
wcout << "wstrA:" << wstrA << ", wstrA.Len :" << wstrA.length() << endl;
//string and wstring的转换
//
/*
方法一:调用Windows的API函数:WideCharToMultiByte()和MultiByteToWideChar()函数
方法二:使用ATL的CA2W类和CW2A类,或者使用A2W宏与W2A宏
方法三:跨平台的方法,使用CRT库的mbstowcs()和wcstombs() mbs是多字节的意思,wcs是单字节的意思
*/
cout << "cstr1:" << cstr1 << ", nCount :" << nCount << endl;
}
catch(...)
{
cout << "adfadfefasdfadf err";
}
return 0;
}