在Linux上,我正在使用GNU gcc 4.9.2版本,并在尝试打印指定长度的零填充字符串时出现奇怪的意外行为.这是我正在尝试的代码段:
#include <cstdio>
#include <cstring>
int main()
{
char buff[5];
sprintf(buff,"%04s","12");
printf("%s\n", buff);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
虽然http://www.cplusplus.com/reference/cstdio/printf/中给出的文档清楚地指出,当指定填充时,标志左边用数字填充(0)而不是空格.但是,它的打印空间填充"12"即"12"而不是"0012".补救?
在下面给出的示例程序中(来源:http ://www.cplusplus.com/reference/unordered_map/unordered_map/rehash/ )
// unordered_map::rehash
#include <iostream>
#include <string>
#include <unordered_map>
int main ()
{
std::unordered_map<std::string,std::string> mymap;
mymap.rehash(20);
mymap["house"] = "maison";
mymap["apple"] = "pomme";
mymap["tree"] = "arbre";
mymap["book"] = "livre";
mymap["door"] = "porte";
mymap["grapefruit"] = "pamplemousse";
std::cout << "current bucket_count: " << mymap.bucket_count() << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出变为:
current bucket_count: 23
Run Code Online (Sandbox Code Playgroud)
为什么桶数变成23?对堆大小有什么影响?堆分配什么时候完成?在存储桶重新散列上还是在实际插入上?动态释放何时完成?何时clear()使用或erase()两者都使用?
出于某种原因,我想使用扩展.cxy来用作c ++源代码.比如,文件名是abc.cxy.但是,我的g++(4.9.2版本)无法编译.我正在编译为:
g++ -o abc.oxy abc.cxy
Run Code Online (Sandbox Code Playgroud)
它抱怨为:g++: warning: conn.cxy: linker input file unused because linking not done
并且,目标文件abc.oxy没有被制作.然而,如果我有扩展名.cxx,并编译为:
g++ -o abc.oxy abc.cxx
Run Code Online (Sandbox Code Playgroud)
它正在制作abc.oxy
我不允许使用除以外的扩展名.c, .cpp, .cxx吗?
在C++中,如果我有一个动态分配的基本类型数组,是否有必要使用delete []来防止内存泄漏?例如,
char * x = new char[100];
delete x; // is it required to call delete [] x?
struct A {
...
};
A *p = new A[30];
delete [] p; // delete p would cause memory leakage
Run Code Online (Sandbox Code Playgroud)
请评论.
c++ memory-leaks new-operator dynamic-memory-allocation delete-operator
我有以下python代码片段:
LL=[]
for i in range(3):
LL.append("a"+str(i))
print LL
Run Code Online (Sandbox Code Playgroud)
输出如下:
['a0', 'a1', 'a2']
Run Code Online (Sandbox Code Playgroud)
如何打印(使用print LL):
[a0, a1, a2]
Run Code Online (Sandbox Code Playgroud)
即没有引号?如果我使用以下代码:
print "[",
for i in range (len(LL)-1):
print LL[i] + ", ",
print LL[i+1]+"]"
Run Code Online (Sandbox Code Playgroud)
这打印 [a0, a1, a2]
c++ ×3
g++4.9 ×2
c ×1
c++11 ×1
heap-memory ×1
list ×1
memory-leaks ×1
new-operator ×1
python ×1
python-2.7 ×1