我使用VS2008编写了以下程序:
#include <fstream>
int main()
{
std::wofstream fout("myfile");
fout << L"???????? ?????? Österreich ?????? ????" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试编译它时,IDE问我是否要将我的源文件保存为unicode,我说"是的,请".
然后我运行程序,myfile出现在我项目的文件夹中.我用记事本打开它,文件是空的.我记得记事本只支持ASCII数据.我用写字板打开它,它仍然是空的.最后,我内心的小天才敦促我查看文件大小,毫不奇怪它是0字节.所以我重建并重新启动了程序,没有任何效果.最后,我决定向StackOverflow上非常聪明的人询问我缺少的东西,我在这里:)
编辑:
在上述聪明人留下一些评论之后,我决定按照他们的建议重写这个程序:
#include <fstream>
#include <iostream>
int main()
{
std::wofstream fout("myfile");
if(!fout.is_open())
{
std::cout << "Before: Not open...\n";
}
fout << L"???????? ?????? Österreich ?????? ????" << std::endl;
if(!fout.good())
{
std::cout << "After: Not good...\n";
}
}
Run Code Online (Sandbox Code Playgroud)
建造它.跑吧.并且...控制台清楚地读到了,令我惊讶的是:"之后:不好......".所以我编辑了我的帖子以提供新的信息,并开始等待答案,这可以解释为什么这是我能做什么.:)
例如,我可以使用std::ostream十六进制的对象输出整数
std::cout << std::hex << 0xabc; //prints `abc`, not the base-10 representation
Run Code Online (Sandbox Code Playgroud)
所有基地都有通用的操纵器吗?就像是
std::cout << std::base(4) << 20; //I want this to output 110
Run Code Online (Sandbox Code Playgroud)
如果有,那我就没有其他问题了.如果没有,那我可以写一个吗?它不会要求我访问私人实施细节std::ostream吗?
请注意,我知道我可以编写一个带有数字的函数,并将其转换为字符串,该字符串是任何基数中该数字的表示.或者我可以使用已经存在的.我问的是自定义流操纵器 - 它们可能吗?
提前致谢
可能重复:
从迭代器获取const_iterator
我想写一元函数返回相应的const_iterator从iterator
template <class Iterator>
struct get_const_iterator
{
typedef ??? type;
};
Run Code Online (Sandbox Code Playgroud)
get_const_iterator<int*>::type 一定是 const int*get_const_iterator<const int*>::type 一定是 const int* get_const_iterator<int* const>::type必须是const int*或const int* const,我不在乎get_const_iterator<std::list<char>::iterator>::type 一定是 std::list<char>::const_iterator等等
可以使用iterator_traits或不使用它们吗?
编辑:我们假设如果2个容器具有相同的iterator类型,那么它们也具有相同的const_iterator类型.我认为这是一个合理的假设,虽然理论上并不完全正确.
const struct sockaddr FAR* name,
Run Code Online (Sandbox Code Playgroud) 如今,我正在阅读APUE.我发现函数定义如下:
void (*signal(int signo, void (*func)(int)))(int);
Run Code Online (Sandbox Code Playgroud)
我很困惑,我知道signal是指向函数的指针,而last(int)是他的参数.我不知道是什么(int signo,void(*func)(int)).
标准是否保证以下代码可以工作(假设st不为空)?
#include <vector>
#include <stack>
int main()
{
extern std::stack<int, std::vector<int> > st;
int* end = &st.top() + 1;
int* begin = end - st.size();
std::vector<int> stack_contents(begin, end);
}
Run Code Online (Sandbox Code Playgroud) 我正在制作一个读取二进制文件的程序.因此,我将单个字节读入无符号字符(实际上将数据读取为字符并将其转换为每个字符的无符号字符).现在我必须将未签名的字符写回二进制文件.
问题是,现在我被迫在将它们转换为字符后写入单个字节(因为二进制文件的write()需要char*buffer).所以,现在我必须做以下事情:
for(int x=0; x<data_size; x++)
{
ch=(char)data[x];
outfile.write(&ch,1);
}
Run Code Online (Sandbox Code Playgroud)
有没有什么方法可以解决这个问题,以便在读写时减少I/O操作量?
如果catch()中允许多个异常,那么它将减少冗余错误处理代码的数量.例如,
try{
// some statments
}
catch(Type1Exception t1, Type2Exception t2, Type3Exception t3) { // wish if this could be allowed
/* t1, t2, t3 are children of Exception and needs same error handling then why to have different catch blocks with same piece of code */
}
Run Code Online (Sandbox Code Playgroud) 我刚刚接受了C++测试,我得到了以下错误的问题:
问:以下程序的输出是什么?
#include <iostream>
#include <stdint.h>
using namespace std;
int main() {
int a = 0;
for (int8_t i = 1; i > 0; i <<= 1)
a++;
cout << a;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
有以下答案可供选择
" 正确 "答案是7.如果答案中有"实施定义的行为",我会选择,所以我选择了最接近的未定义行为.据我所知,在符号和大写,1的补码和2的补码中,答案将是7.但是C++标准理论上是否允许任何其他数字表示?例如,符号和幅度,但0表示否定?
我是否正确,这个问题的真正正确答案应该是实施定义的行为,如果没有,你可以解释为什么答案是7而不管实施情况如何?
我读了这个问题的评论,看起来最初a是char 的类型,显然已经引起了很多关于是否char签名的抱怨,所以测试人员将其改为int8_t.作为奖金问题,是<stdint.h>C++的一部分吗?O_O
我正在玩一些图形,我用箭头键实现了简单的相机移动.我的第一种方法是覆盖keyPressEvent做这样的事情:
switch(key)
{
case up: MoveCameraForward(step); break;
case left: MoveCameraLeft(step); break;
...
}
Run Code Online (Sandbox Code Playgroud)
这不符合我的意愿.当我按住(例如)前进键时,相机向前移动"步进"单位,然后暂停一段时间然后继续移动.我猜这是事件的生成方式,以避免在有点长按键的情况下发生多个事件.
所以,我需要在Paint()日常工作中轮询键盘.我还没有找到如何用Qt做到这一点.我想到了一个map<Key, bool>将更新的内容keyPressEvent并在其中keyReleaseEvent轮询该地图Paint().有更好的想法吗?感谢您的任何见解.