当string字符串混合有英文和文字的时候,怎么将他们逐个输出

C++语言 码拜 8年前 (2016-04-20) 1077次浏览
①最近在学数据结构,老师让做一个哈夫曼压缩小工具,用的是32位控制台,C++,输入的是一个.txt文本的路径
②通过在网上搜索学习,就大致初步知道了<fstream>的几种用法,其中本人用的是逐行读取,将读取的文本放入字符串中,通过这几串代码就把文本都放进去了str字符串

string name, str;
cin >> name;
ifstream fin(name);
while (!fin.eof())
{
	string temp;
	getline(fin, temp);
	str = str + "\n" + temp;
}

③然后本人需要就是统计每个字符在文本中出现了多少次,去算出它们的权值,假如全是英文的时候,本人用str.length()算出长度,然后遍历str[i],已经成功实现。后来加上中文的时候,发现输出和统计不了中文,然后通过网上查找就知道了原来string的本质是字符数组,一个汉字需要2个char型才能储存,因此,通过str.length()统计出来的长度并不准确,而且通过遍历的话,也没有办法准确输出汉字所对应的码
④后来也发现了有wstring宽字符串,应该是相当于wchar_t []的用法,但是在fstream的方法getline中,wstring并不是可接收的参数,所以也没有办法实现。
⑤所以当有英文和文字的文本的时候,通过str[i]的话,英语是1个i,汉字则是2个i,本人应该怎么去算出每个字的权值,求问各位高手给出简单可行的方案当string字符串混合有英文和文字的时候,怎么将他们逐个输出

解决方案

60

最简单方便的就是 string 转成 wstring

10

String Manipulation
These routines operate on null-terminated single-byte character, wide-character, and multibyte-character strings. Use the buffer-manipulation routines, described in Buffer Manipulation, to work with character arrays that do not end with a null character.
String-Manipulation Routines
Routine Use
_mbscoll, _mbsicoll, _mbsncoll, _mbsnicoll Compare two multibyte-character strings using multibyte code page information (_mbsicoll and _mbsnicoll are case-insensitive)
_mbsdec, _strdec, _wcsdec Move string pointer back one character
_mbsinc, _strinc, _wcsinc Advance string pointer by one character
_mbslen Get number of multibyte characters in multibyte-character string; dependent upon OEM code page
_mbsnbcat Append, at most, first n bytes of one multibyte-character string to another
_mbsnbcmp Compare first n bytes of two multibyte-character strings
_mbsnbcnt Return number of multibyte-character bytes within supplied character count
_mbsnbcpy Copy n bytes of string
_mbsnbicmp Compare n bytes of two multibyte-character strings, ignoring case
_mbsnbset Set first n bytes of multibyte-character string to specified character
_mbsnccnt Return number of multibyte characters within supplied byte count
_mbsnextc, _strnextc, _wcsnextc Find next character in string
_mbsninc. _strninc, _wcsninc  Advance string pointer by n characters
_mbsspnp, _strspnp, _wcsspnp Return pointer to first character in given string that is not in another given string
_mbstrlen Get number of multibyte characters in multibyte-character string; locale-dependent
sprintf, _stprintf Write formatted data to a string
strcat, wcscat, _mbscat Append one string to another
strchr, wcschr, _mbschr Find first occurrence of specified character in string
strcmp, wcscmp, _mbscmp Compare two strings
strcoll, wcscoll, _stricoll, _wcsicoll, _strncoll, _wcsncoll, _strnicoll, _wcsnicoll Compare two strings using current locale code page information (_stricoll, _wcsicoll, _strnicoll, and _wcsnicoll are case-insensitive)
strcpy, wcscpy, _mbscpy Copy one string to another
strcspn, wcscspn, _mbscspn,  Find first occurrence of character from specified character set in string
_strdup, _wcsdup, _mbsdup Duplicate string
strerror Map error number to message string
_strerror Map user-defined error message to string
strftime, wcsftime Format date-and-time string
_stricmp, _wcsicmp, _mbsicmp Compare two strings without regard to case
strlen, wcslen, _mbslen, _mbstrlen Find length of string
_strlwr, _wcslwr, _mbslwr Convert string to lowercase
strncat, wcsncat, _mbsncat Append characters of string
strncmp, wcsncmp, _mbsncmp Compare characters of two strings
strncpy, wcsncpy, _mbsncpy Copy characters of one string to another
_strnicmp, _wcsnicmp, _mbsnicmp Compare characters of two strings without regard to case
_strnset, _wcsnset, _mbsnset Set first n characters of string to specified character
strpbrk, wcspbrk, _mbspbrk Find first occurrence of character from one string in another string
strrchr, wcsrchr,_mbsrchr Find last occurrence of given character in string
_strrev, _wcsrev,_mbsrev Reverse string
_strset, _wcsset, _mbsset Set all characters of string to specified character
strspn, wcsspn, _mbsspn Find first substring from one string in another string
strstr, wcsstr, _mbsstr Find first occurrence of specified string in another string
strtok, wcstok, _mbstok Find next token in string
_strupr, _wcsupr, _mbsupr Convert string to uppercase
strxfrm, wcsxfrm Transform string into collated form based on locale-specific information
vsprintf, _vstprint Write formatted output using a pointer to a list of arguments

30

用GB编码汉字还是2字节,你用utf-8编码就不一定是2字节。
最简单的就是像ls说的,用wstring。
不然你得先知道用的是什么编码,然后用现成的转换函数转成wstring(ucs编码)或本人写区分的代码。

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明当string字符串混合有英文和文字的时候,怎么将他们逐个输出
喜欢 (0)
[1034331897@qq.com]
分享 (0)