import Image
import os
for dirname, dirs, files in os.walk("."):
for filename in files:
try:
im = Image.open(os.path.join(dirname,filename))
except IOError:
print "error opening file :: " + os.path.join(dirname,filename)
print im.size
Run Code Online (Sandbox Code Playgroud)
在这里,我试图打印目录(和子)中所有文件的大小.但我知道im在排队时超出范围im.size.但是如果不使用else或finally阻止我怎么办呢.
显示以下错误:
Traceback (most recent call last):
File "batch.py", line 13, in <module>
print im.size
NameError: name 'im' is not defined
Run Code Online (Sandbox Code Playgroud) 我试图使用clang-tools特别是clang-format在vim中进行自动代码格式化,但我找不到这个工具使用apt-get搜索.
以前是否有人遇到过此问题,您有什么建议吗谢谢
最近我测试了一些C++深度和暗角,我对一个微妙的点感到困惑.我的测试实际上非常简单:
// problem 1
// no any constructor call, g++ acts as a function declaration to the (howmany())
// g++ turns (howmany()) into (howmany(*)())
howmany t(howmany());
// problem 2
// only one constructor call
howmany t = howmany();
Run Code Online (Sandbox Code Playgroud)
我对上线的期望是; 第一个howmany()构造函数调用将生成一个临时对象,然后编译器将使用该临时对象和copy-constructor来实例化t.但是,编译器的输出确实让我困惑,因为输出只显示一个构造函数调用.我的朋友们提到了我关于编译器传值优化的问题,但我们对此并不确定.我想了解这里发生了什么?
问题2的输出如下.问题1完全超出了对象实例化,因为编译器将其表现为函数指针声明.
howmany()
~howmany()
Run Code Online (Sandbox Code Playgroud)
我的测试类是:
class howmany {
public:
howmany() {
out << "howmany()" << endl;
}
howmany(int i) {
out << "howmany(i)" << endl;
}
howmany(const howmany& refhm) {
out << "howmany(howmany&)" << endl;
}
howmany& …Run Code Online (Sandbox Code Playgroud) 在C++中,列表数据结构具有合并功能,该功能基本上删除源列表中的所有对象并放入目标列表.
// source list will be empty after this operation
destinationList.merge(sourceList);
Run Code Online (Sandbox Code Playgroud)
根据教程/示例,必须在合并操作之前对列表进行排序.
destinationList.sort();
sourceList.sort();
destinationList.merge(sourceList);
Run Code Online (Sandbox Code Playgroud)
我很困惑,因为如果需要排序列表,为什么C++不会通过在合并函数中调用sort函数来强制执行它?
另外一件事,我可以先合并未排序的列表,然后我可以对合并列表进行排序,是不是也一样?
destinationList.merge(sourceList);
destinationList.sort();
Run Code Online (Sandbox Code Playgroud) 我现在正在回顾C++中的微妙观点.我发现了一个有趣的问题.你可以检查一下,并分享你为什么这样工作的原因.
谢谢
const int x = 5;
void func() {
// ! Error
// int x = x;
// ! Fine
int x[x];
x[0] = 12;
cout << x[0];
}
Run Code Online (Sandbox Code Playgroud) 我一次又一次看到这个烦人的问题。您能否分享您的知识,这可能有助于我们找到答案。
我的困惑是,正斜杠是posix标准,但操作系统的目录结构不同。
谢谢
(例如,您将传递给std :: fstream:open()的字符串以打开文件。)
A.“ ::目录:file.bin”
B.“ C:/Directory/File.bin”
C。“ / directory / file.bin”
D.“ C://Directory//File.bin”
E. std:fstream文件路径不可移植。
在下面的代码中,作者指出new operator函数调用可能会导致异常,因此此实现不是异常安全的,因为对象状态已在第一行中更改。
String &String::operator =( const char *str ) {
// state is changed
delete [] s_;
if( !str ) str = "";
// exception might occur because of new operator
s_ = strcpy( new char[ strlen(str)+1 ], str );
return *this;
}
Run Code Online (Sandbox Code Playgroud)
在阅读时,我想知道 C 库函数会在 C++ 中抛出异常吗?我知道 C 中没有例外,但由于我们使用的是 C++ 编译器,因此可能会有例外。
那么,我们可以将 C 标准库函数视为异常安全的函数调用吗?
谢谢你。
顺便说一句,为了记录,实现上述功能的正确方法(异常安全)如下。
String &String::operator =( const char *str ) {
if( !str ) str = "";
char *tmp = strcpy( new char[ strlen(str)+1 …Run Code Online (Sandbox Code Playgroud) c++ exception exception-safe exception-safety c-standard-library