当我声明一个大小为2的char数组并在有一个尾随的'\ 0'(空字符)时为其分配数据?
我知道:
char data[2] = {'a', 'b'}; // array holds 'a', 'b'
Run Code Online (Sandbox Code Playgroud)
我也知道
char data[] = "ab"; // array holds 'a', 'b', '\0'
Run Code Online (Sandbox Code Playgroud)
但是,我想知道这是做什么的?
char data[2] = "ab"; // Is there a trailing '\0'?
Run Code Online (Sandbox Code Playgroud)
我一直以为这是一个错误但是看着cppreference 它说:
如果数组的大小已知,则它可能比字符串文字的大小小一个,在这种情况下,将忽略终止空字符:
char str[3] = "abc"; // str has type char[3] and holds 'a', 'b', 'c'
Run Code Online (Sandbox Code Playgroud)
那么"可能"是什么意思呢?它是依赖于实现的吗?
想知道空表达式是否评估为NOP或者它是否依赖于编译器.
// Trivial example
int main()
{
;;
}
Run Code Online (Sandbox Code Playgroud) 我想在Python中"包装"C++中的列表/向量.基本上我想将元素从列表末尾切换到列表的开头.我不想明确地制作新的清单.
在Python中我可以写如下:
my_list = [1, 2, 3, 4, 5]
#[1, 2, 3, 4, 5]
q = collections.deque(my_list)
q.rotate(3)
#deque([3, 4, 5, 1, 2])
Run Code Online (Sandbox Code Playgroud)
我在STL看了deque,但是我没有看到任何类似旋转的东西.似乎应该有一个简单的方法来使用迭代器或类似的东西.
将一些遗留C代码从QNX(Photon C编译器)移植到Linux(GCC).在几个地方,我看到这样的代码:
void process_data(char key, char *data)
{
int i;
/* Required for compilation */
i=i;
key=key;
data=data;
...
}
Run Code Online (Sandbox Code Playgroud)
显然,这个代码不是编译所必需的,它什么都不做.
我的问题是你为什么要这样做呢?
我有一个string嵌入'\0'字符的c ++ .
我有一个函数replaceAll(),它应该用另一个模式替换所有出现的模式.对于"普通"字符串,它工作正常.但是,当我试图找到'\0'角色时,我的功能不起作用,我不知道为什么.replaceAll似乎失败对string::find()我来说没有意义.
// Replaces all occurrences of the text 'from' to the text 'to' in the specified input string.
// replaceAll("Foo123Foo", "Foo", "Bar"); // Bar123Bar
string replaceAll( string in, string from, string to )
{
string tmp = in;
if ( from.empty())
{
return in;
}
size_t start_pos = 0;
// tmp.find() fails to match on "\0"
while (( start_pos = tmp.find( from, start_pos …Run Code Online (Sandbox Code Playgroud)