在以前版本的Visual Studio中,使用_sleep或strncpy等函数只会输出警告.在最新版本中,突然出现错误:

错误C4996:'_sleep':此函数或变量已被更新的库或操作系统功能取代.考虑使用Sleep.详细信息请参见在线帮助.
我知道我可以通过添加#pragma warning(disable: 4996)代码的开头来禁用它,但是VS试图强迫我使用其他功能是非常烦人的.有没有办法禁用这种行为?
在你问之前,"将警告视为错误"被禁用,即使我关闭所有警告也会出错!
我正在寻找一种简单的方法来检查某个字符串是否是正确拼写的英文单词.例如,'looking'将返回True,而'hurrr'将返回False.我不需要拼写建议或任何拼写纠正功能.只是一个简单的函数,它接受一个字符串并返回一个布尔值.
我已经阅读了一堆文章和论坛帖子讨论这个问题所有的解决方案似乎太复杂了这么简单的任务.
以下是来自cplusplus.com的示例代码:
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
只要example.txt只有ASCII字符,它就可以正常工作.如果我试图用俄语添加一些东西,事情会变得混乱.
在GNU/Linux中,它就像将文件保存为UTF-8一样简单.
在Windows中,这不起作用.将文件转换为UCS-2 Little Endian(默认情况下Windows似乎使用)并将所有函数更改为wchar_t对应文件也不起作用.
如果没有进行各种魔术编码转换,是不是有某种"正确"的方法来完成这项工作?
让我们说吧
s = u"test\u0627\u0644\u0644\u0647 \u0623\u0643\u0628\u0631\u7206\u767A\u043E\u043B\u043E\u043B\u043E"
Run Code Online (Sandbox Code Playgroud)
如果我尝试直接打印,
>>> print s
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'cp932' codec can't encode character u'\u0627' in position 4: illegal multibyte sequence
Run Code Online (Sandbox Code Playgroud)
所以我在Python中将控制台更改为UTF-8(否则它将无法理解我的输入).
import win32console
win32console.SetConsoleOutputCP(65001)
win32console.SetConsoleCP(65001)
Run Code Online (Sandbox Code Playgroud)
然后输出编码为utf-8的字符串,因为Python不知道chcp 65001是UTF-8(一个已知的bug).
>>> print s.encode('utf-8')
test???? ???????????Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 0] Error
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,它会成功打印,直到它到达换行符,然后它会抛出IOError.
以下解决方法有效:
def safe_print(str):
try:
print str.encode('utf-8')
except:
pass
print
>>> safe_print(s)
test???? ???????????
Run Code Online (Sandbox Code Playgroud)
但必须有更好的方法.有什么建议?
我的理解是 Wine 直接执行包含在 PE 可执行文件中的机器代码。假设这是正确的,如果机器代码包含一个系统调用,Linux 显然无法理解会发生什么?Wine 会以某种方式拦截他们吗?如果是这样,它究竟是如何工作的?
我试图在 Wine 的源代码中找到答案,但发现它令人生畏。我什至找不到机器代码实际执行的地方。
考虑这个代码:
template <typename T>
class Singleton
{
};
class Logger : public Singleton<Logger> {
friend class Singleton;
};
Run Code Online (Sandbox Code Playgroud)
它在 gcc 和 clang 中编译,但它有效吗?[temp.local].1 说:
当它与模板参数列表一起使用时,作为模板模板参数的模板参数,或作为朋友类模板声明的详细类型说明符中的最终标识符,它是一个模板名称指的是类模板本身。
粗体部分似乎适用,并且朋友声明似乎需要类型名称而不是模板名称(参见 [class.friend])。
编译器错了还是我误读了标准?
c++ friend-class language-lawyer class-template injected-class-name
我创建了一组C字符串,提供我自己的比较器函数,因为我希望它只考虑前三个字符.这是它的定义:
struct set_object {
bool operator()(const char* first, const char* second) {
return strncmp(first, second, 3) > 0;
}
};
std::set<const char*, set_object> c_string_set;
Run Code Online (Sandbox Code Playgroud)
它按我想要的方式工作,在我按照我在set_object类中概述的方式添加它们时对字符串进行排序.但是当我尝试添加一个比较等于已经添加的字符串的字符串时,有趣的部分开始了.例如,如果我在集合中已经存在"aaa"时尝试添加"aaab",则不会将其添加到集合中.如果我先添加"aaab",然后尝试添加"aaa",它只列出"aaab".但是如果我只提供了一个函数,当其中一个字符串更大时返回true,它怎么知道它们何时相等?它应该返回false时,它要么等于或小!
澄清一下,这不是问题,只是想弄清楚C++是如何工作的.
Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
# RAM usage: 2100
>>> class Test:
... def __init__(self, i):
... self.one = i
... self.hundred = 100*i
...
# RAM usage: 2108
>>> list1 = [ Test(i) for i in xrange(10000) ]
# RAM usage: 4364
>>> del(list1)
# RAM usage: 2780
>>> list2 = [ {"one": i, "hundred": 100*i} for i in xrange(10000) ] …Run Code Online (Sandbox Code Playgroud) 以下代码在GCC,Clang和Visual Studio中失败:
#include <string>
#include <sstream>
int main() {
std::string s = "hello"; // ok, copy-initialization
std::stringstream ss1(s); // ok, direct-initialization
std::stringstream ss2 = s; // error
}
Run Code Online (Sandbox Code Playgroud)
我认为直接初始化的唯一情况是复制初始化不起作用的时候构造函数是显式的,在这种情况下不是这样.这是怎么回事?
我想创建一个,以便我可以使用set :: find检查某个单词是否在集合中
但是,C字符串是指针,因此默认情况下,该集合将通过指针值对它们进行比较.要正常运行,必须取消引用它们并比较字符串.
我可以将构造函数指向strcmp()函数作为比较器,但这并不是我想要的工作方式.我可能要检查的单词可能是更长字符串的一部分,我不想因为性能问题而创建新字符串.如果没有集合,我会使用strncmp(a1,a2,3)来检查前3个字母.事实上,3可能是最长的,所以我可以将第三个参数保持不变.
有没有办法构建一个通过调用strncmp()来比较其元素的集合?我们非常感谢代码示例.
这是我想要做的伪代码:
bool WordInSet (string, set, length)
{
for (each word in set)
{
if strncmp(string, word, length) == 0
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
但我更喜欢使用标准库函数来实现它.