C ++函数返回带逗号的字符串

时间:2013-10-12 19:42:47

标签: c++ return comma

我试图将输入:“²³”变为“2,3”,如果不是“²³”则返回“否”

预期结果:

  

输入密钥:²³   翻译:2,3,

真实结果:

  

输入密钥:²³   翻译:NoNoNoNo

代码:

#include <iostream>
#include <string>   
#include <vector>  
#include <cmath>  



// To Compile: g++ x.cpp -o x

using namespace std;

//Define the functions

string convert( string text);


int main()
{

string d;
string input;
string cleartext;
cout << "Enter the key: ";
cin >> input;


vector <string> key ( input.size() );        // Make a vector with the size of their input
for (int i = 0; i < input.size(); i++)       // Fill the vector with the characters given
    {
        key.at(i)=input[i]; 
        d = convert( key[i]);
        cleartext.append(d);
    }
cout << "Translated: " << cleartext << endl;
return 0;
}




string convert( string text)
{
if (text == "²") 
{
return "2,";
} 
if (text == "³") 
{
return "3,";
}
return "No";
}

我无法让它正常运行而且我很难过。我猜它必须用逗号做一些事情,任何人都可以添加一些见解吗?

1 个答案:

答案 0 :(得分:0)

这看起来好像是在处理UTF-8编码数据,希望每个Unicode代码点都适合char。鉴于Unicode使用至少20位(上次我对这些主题感兴趣;我很自信,因为他们已经转移到24位,但我没有验证这个主张),这将无法正常工作。当使用UTF-8表示时,你的字符SUPERSCRIPT TWO(U + 00B2)和SUPERSCRIPT THREE(U + 00B3)将各使用两个字节。鉴于两个字符的输入导致“否”被打印四次,这几乎可以支持这种猜测。

处理Unicode时,最好使用宽字符串,例如std::basic_string<char32_t>,但我不认为需要定义此字符类型的流。只要你单独处理特殊字符,你就可以在大多数情况下使用std::wstring离开,尽管wchar_t在某些平台上只使用16位,因此在这种情况下将使用UTF-16编码字符。