嗨,我试图将unicode字符串输出到带有iostreams的控制台并失败.
我发现了这一点: 在c ++控制台应用程序中使用unicode字体 ,这个代码片段有效.
SetConsoleOutputCP(CP_UTF8);
wchar_t s[] = L"èéøÞ????æ?a";
int bufferSize = WideCharToMultiByte(CP_UTF8, 0, s, -1, NULL, 0, NULL, NULL);
char* m = new char[bufferSize];
WideCharToMultiByte(CP_UTF8, 0, s, -1, m, bufferSize, NULL, NULL);
wprintf(L"%S", m);
Run Code Online (Sandbox Code Playgroud)
但是,我没有找到任何方法来使用iostream正确输出unicode.有什么建议?
这不起作用:
SetConsoleOutputCP(CP_UTF8);
utf8_locale = locale(old_locale,new boost::program_options::detail::utf8_codecvt_facet());
wcout.imbue(utf8_locale);
wcout << L"¡Hola!" << endl;
Run Code Online (Sandbox Code Playgroud)
编辑 我找不到任何其他解决方案,而不是在流中包装此片段.希望,有人有更好的想法.
//Unicode output for a Windows console
ostream &operator-(ostream &stream, const wchar_t *s)
{
int bufSize = WideCharToMultiByte(CP_UTF8, 0, s, -1, NULL, 0, NULL, NULL);
char *buf = …Run Code Online (Sandbox Code Playgroud) 是否有任何方法可以根据所使用的操作系统自动使用正确的EOL字符?
我在想什么std::eol?
我知道使用预处理程序指令非常容易,但如果已经可用则很好奇.
我感兴趣的是我通常在我的应用程序中有一些消息,我后来将它们组合成一个字符串,我希望用EOL分隔它们.我知道我可以使用,std::stringstream << endl但有时似乎是一种矫枉过正而不是常规追加.
如果我为我的编译器启用OpenMP,是否可以并行执行任何STL算法/容器操作,如std :: fill,std :: transform?我目前正在与MSVC 2008合作.或者也许有其他方法使它并发?
谢谢.
我想在我的软件中使用类的前向声明,所以我可以使用typedef
并在类完全声明中使用它们.
像这样的Smth:
class myclass;
typedef boost::shared_ptr<myclass> pmyclass;
typedef std::list<pmyclass > myclasslist;
class myclass : public baseclass
{
private: // private member declarations
__fastcall myclass();
public: // public member declarations
__fastcall myclass(myclass *Parent)
: mEntry(new myclass2())
{
this->mParent = Parent;
}
const myclass *mParent;
myclasslist mChildren;
boost::scoped_ptr<myclass2> mEntry;
};
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:这种方法有什么缺点吗?我记得有关前向声明的析构函数问题的一些讨论,但我并没有把所有东西都拿出来.
还是有其他选择来实现这样的东西?
谢谢.
编辑:我找到了我所指的讨论:这里
我正在尝试使用boost字符串算法进行不区分大小写的搜索.
总新手在这里.
如果我这样使用它,我会收到错误.
std::string str1("Hello world");
std::string str2("hello");
if ( boost::ifind_first(str1, str2) ) some code;
Run Code Online (Sandbox Code Playgroud)
转换为char指针可以解决问题.
boost::ifind_first( (char*)str1.c_str(), (char*)str2.c_str() );
Run Code Online (Sandbox Code Playgroud)
有没有办法直接搜索std :: string对象?
此外,也许还有另一种方法可以知道字符串是否存在于另一个字符串中,并且不区分大小写的搜索?
抱歉,如果愚蠢但无法找到答案.
#include <iostream>
using namespace std;
int main()
{
double a(0);
double b(0.001);
cout << a - 0.0 << endl;
for (;a<1.0;a+=b);
cout << a - 1.0 << endl;
for (;a<10.0;a+=b);
cout << a - 10.0 << endl;
cout << a - 10.0-b << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
0
6.66134e-16
0.001
-1.03583e-13
尝试使用MSVC9,MSVC10,Borland C++ 2010进行编译.所有这些都到达了大约1e-13的错误.只有1000,10000增量才能产生如此显着的误差累积是否正常?
我想我在这个问题的理论背景中错过了.我知道有类似的帖子,但我仍然没有得到它.
我有这样的代码:
void somefunc1(Word &Key)
{
somefunc2(Key);
}
void somefunc2(char &char1)
{
return;
}
Run Code Online (Sandbox Code Playgroud)
编译器在这里生成一个错误:
somefunc2(Key);
Run Code Online (Sandbox Code Playgroud)
[BCC32错误] Unit1.cpp(830):E2357使用'unsigned short'初始化的引用,需要'char'类型的左值
我发现这是因为ANSI 2003对C++处理临时和引用的规定,但我仍然没有弄到这里有什么问题.
当我做c风格转换时:
somefunc2( *(char*)&Key )
Run Code Online (Sandbox Code Playgroud)
它解决了一个问题.
任何人都可以提示我出了什么问题,为什么会出错?
简单的问题:如果我有一个字符串,我想添加头部和尾部字符串(一个在开头,另一个在结尾),最好的方法是什么?像这样的东西:
std::string tmpstr("some string here");
std::string head("head");
std::string tail("tail");
tmpstr = head + tmpstr + tail;
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法呢?
提前致谢.
我刚刚发现了这个组件并开始使用它.
我知道它的整个概念是在需要的时候初始化节点,但我需要所有节点立即初始化.
有什么聪明的方法呢?
我想出的唯一一件事是在添加节点后使用GetLast().
我相信,还有更好的方法吗?
相当简单的问题.我应该在哪里存储错误,异常,用户消息?到目前为止,我总是在函数内部声明本地字符串,它将被调用并且没有打扰.例如
SomeClass::function1(...)
{
std::string str1("message1");
std::string str2("message2");
std::string str3("message3");
...
// some code
...
}
Run Code Online (Sandbox Code Playgroud)
突然间,我意识到每次都会调用构造和初始化,这可能会非常昂贵.将它们作为静态字符串存储在类中甚至是单独的模块中会更好吗?本地化并非如此.
提前致谢.
我有一个在单独的文件中定义的类,在某些时候我需要从另一个源文件访问其中一个公共成员函数.出于某种原因,我忘了怎么做,编译器给我一个错误.
我有classA.h,类A的定义类似于:
class classA {
public:
int function1(int alpha);
}
Run Code Online (Sandbox Code Playgroud)
和一个单独的文件classA.cpp一起实现.然后在其他一些文件blah.cpp中我包含标题并尝试像这样访问它:
classA::function1(15);
Run Code Online (Sandbox Code Playgroud)
并且我的编译器拒绝它,因为它无法找到'classA :: function1(int)'的匹配项.
如果重要的话,我会使用Embarcadero RAD studio 2010.