代码段1:
wchar_t *aString() 
{
     wchar_t *str = new wchar[5];
     wcscpy(str, "asdf\0");
     return str;
}
wchar_t *value1 = aString();
代码段2
wstring wstr = L"a value";
wchar_t *value = wstr.c_str();
如果未删除代码段2中的值,则不会发生内存泄漏.但是,如果未删除代码段1中的value1,则会发生内存泄漏.wstring :: c_str的内部代码对我来说是一样的.
将std :: wstring转换为数字类型的最佳方法是什么,例如int,long,float或double?
我有一个游戏,它使用std :: wstring作为其基本字符串类型在数千个地方以及使用wchar_t及其函数进行操作:wcsicmp()wcslen()vsprintf()等.
问题是R5c不支持wstring(此写入时最新的ndk).
我无法改变代码使用std :: string因为国际化而且我会破坏许多游戏使用的游戏引擎......
我有哪些选择?
1 - 用我自己的字符串类替换字符串和wstring
这将使我更好的平台独立性,但它是荒谬重新实现车轮.我已经开始使用字符串的COW实现了.我需要它是COW,因为我将它们用作hash_maps中的键.这当然是很多工作和容易出错......但似乎这是我能做的事情.
2 - 尝试使用我自己的C标准库(wcslen,mbstowcs ...)的宽字符串函数实现来修复NDK重新编译STLPort
这将是更好的方式......但我不知道该怎么做:(
如何在libstdc ++.a或libstlport_static.a中替换函数(比如说wcslen)?(不确定它们在哪里:()
而且我不确定我需要重新实现哪些功能,我知道wcslen没有工作所以我猜他们应该全部......
3 - 你还有其他想法吗?
我不能等待正式的修复,如果我不知道如何做#2,我将不得不选择#1选项.
我已经读到某个地方,如果你的目标是2.3你可以使用wstrings,但我应该定位Android 2.1.
PS:忘了说我当然需要使用STL,但没有RTTI,我可以无异常地生活.
提前致谢!
如何€áa¢cée£在控制台/屏幕上打印如下字符串?我试过这个:
#include <iostream>    
#include <string>
using namespace std;
wstring wStr = L"€áa¢cée£";
int main (void)
{
    wcout << wStr << " : " << wStr.length() << endl;
    return 0;
}
这是行不通的.即使令人困惑,如果我€从字符串中删除,打印输出就像这样:?a?c?e? : 7但是€在字符串中,€字符后没有任何内容被打印出来.
如果我在python中编写相同的代码:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
wStr = u"€áa¢cée£"
print u"%s" % wStr
它在同一台控制台上正确打印出字符串.我在c ++中缺少什么(好吧,我只是一个菜鸟)?干杯!!
#include <iostream>
#include <string>
using namespace std;
string wStr = "€áa¢cée£";
char *pStr = 0;
int main (void) …我知道要在C++中获取unicode字符,我可以这样做:
std::wstring str = L"\u4FF0";
但是,如果我想获得4FF0到5FF0范围内的所有字符怎么办?是否可以动态构建一个unicode角色?我想到的是像这样的伪代码:
for (int i = 20464; i < 24560; i++ { // From 4FF0 to 5FF0
    std::wstring str = L"\u" + hexa(i); // build the unicode character
    // do something with str
}
我怎么用C++做到这一点?
我的代码中有一个宽字符串(std :: wstring),我需要在其中搜索宽字符.
我使用find()函数:
    wcin >> str;
    wcout << ((str.find(L'?') != wstring::npos)? L"EXIST":L"NONE");
L'?' 是一封西里尔字母.
但是同一个调用中的find()总会返回npos.在拉丁字母的情况下,find()可以正常工作.
这是这个功能的问题吗?或者我做错了什么?
UPD
我使用MinGW并以UTF-8保存源代码.我也设置了语言环境setlocale(LC_ALL, "");.代码相同的wcout << L'?';工作是coorectly.但同样的
wchar_t w;
wcin >> w;
wcout << w;
工作不正确.
它很奇怪.之前我使用setlocale()编码没有问题.
我满足了在控制台中对齐输出文本的要求。输出文本存储为std::wstring并编码为UTF-8. 使此任务变得棘手的是,输出文本同时包含 ASCII 字符和日语字符,例如\xe3\x83\x8a5\xe5\x9b\x9e1\xe3\x82\xb7\xe3\x83\xa7\xe3\x83\xb34a\xe3\x82\xb9F。由于ASCII字符和日文字符的宽度不同,使用setw()或直接无法对齐。\n例如,wprintf(L"%-10s")
#include <iostream>\n#include <iomanip>\nusing namespace std;\nint main(){\n    std::locale::global(std::locale(""));\n    wstring s[] = {L"\xe7\x9f\xad3\xe3\x83\x9e231\xe3\x83\xbc\xe2\x97\x8b",L"\xe3\x81\xae\xe3\x81\x8d3\xe3\x83\xbc\xe3\x83\x8a",L"\xe3\x81\x90\xe3\x83\x9e",L"\xe3\x82\x8d\xe3\x81\xab\xe3\x83\x88"};\n    for(int i=0;i<4;i++)    wcout << setw(10) << s[i] <<123<< endl;\n}\n会像:
\n    \xe7\x9f\xad3\xe3\x83\x9e231\xe3\x83\xbc\xe2\x97\x8b123\n     \xe3\x81\xae\xe3\x81\x8d3\xe3\x83\xbc\xe3\x83\x8a123\n       \xe3\x81\x90\xe3\x83\x9e123\n      \xe3\x82\x8d\xe3\x81\xab\xe3\x83\x88123\n但如果文本仅包含 ASCII 字符,则它可以正常工作。
\n我知道我可以自己编写一个新函数来对齐它,但我想知道是否已经有一个可靠的解决方案。
\n我用C++编写了一个简单的程序,使用了boost :: variant.程序代码如下所示.
    #include <string>
    #include <iostream>
    #include <boost/variant.hpp>
    int main (int argc, char** argv)
    {
        boost::variant<int, std::wstring> v;
        v = 3;
        std::cout << v << std::endl;
        return 0;
    }
但是当我尝试用命令编译它时
g++ main.cpp -o main -lboost_system
我明白了
/usr/include/boost/variant/detail/variant_io.hpp:64: error: no match for ‘operator<<’ in ‘((const boost::detail::variant::printer<std::basic_ostream<char, std::char_traits<char> > >*)this)->boost::detail::variant::printer<std::basic_ostream<char, std::char_traits<char> > >::out_ << operand’
接下来是一堆候选函数.
我错过了什么?有趣的是,当我使用std::string而不是std::wstring一切都很好.
提前致谢.
我有这个std :: string,其中包含一些跨越多个字节的字符.
当我对此字符串执行子字符串时,输出无效,因为当然,这些字符计为2个字符.在我看来,我应该使用wstring,因为它将这些字符存储为一个元素而不是更多.
所以我决定将字符串复制到wstring中,但是当然这没有意义,因为字符仍然分为2个字符.这只会让情况变得更糟.
将字符串转换为wstring有一个很好的解决方案,将特殊字符合并为1个元素而不是2个元素.
谢谢
我在 C++ 中有一个 wstring 变量和一个 string 变量。我想将它们连接起来,但简单地将它们添加在一起会产生构建错误。我怎样才能将它们结合起来?如果我需要将 wstring 变量转换为字符串,我将如何完成此操作?
//A WCHAR array is created by obtaining the directory of a folder - This is part of my C++ project
WCHAR path[MAX_PATH + 1] = { 0 };
GetModuleFileNameW(NULL, path, MAX_PATH);
PathCchRemoveFileSpec(path, MAX_PATH);
//The resulting array is converted to a wstring
std::wstring wStr(path);
//An ordinary string is created
std::string str = "Test";
//The variables are put into this equation for concatenation - It produces a build error
std::string result = wStr …