新的格式允许我们这样做:'{:.<12}'.format('##')- 可选的填充字符.
我们可以使用旧格式吗?
(我知道我们可以填空格'%-12s' % '##')
此外,旧的格式允许我们这样做:'%-*s' % (12, '##')- 可变长度.
我们可以使用新的格式吗?
#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(0); cin.tie(0);
auto arr = new int[5];
// int arr[5] = {1, 2, 3, 4, 5};
for (auto i: arr){
cout << i << ' ';
}
}
Run Code Online (Sandbox Code Playgroud)
为什么这不起作用?我收到编译时错误说这个.
C.cpp: In function 'int main()':
C.cpp:8:15: error: 'begin' was not declared in this scope
for (auto i: arr){
^
C.cpp:8:15: note: suggested alternatives:
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/x86_64-pc-cygwin/bits/stdc++.h:94:0,
from C.cpp:1:
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/valarray:1206:5: note: 'std::begin'
begin(const valarray<_Tp>& __va)
^
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/valarray:1206:5: note: 'std::begin'
C.cpp:8:15: error: 'end' was …Run Code Online (Sandbox Code Playgroud) 考虑以下Python 2代码
from timeit import default_timer
def floor():
for _ in xrange(10**7):
1 * 12 // 39 * 2 // 39 * 23 - 234
def normal():
for _ in xrange(10**7):
1 * 12 / 39 * 2 / 39 * 23 - 234
t1 = default_timer()
floor()
t2 = default_timer()
normal()
t3 = default_timer()
print 'Floor %.3f' % (t2 - t1)
print 'Normal %.3f' % (t3 - t2)
Run Code Online (Sandbox Code Playgroud)
在我的计算机上,输出是
Floor 0.254
Normal 1.766
Run Code Online (Sandbox Code Playgroud)
那么,当两个人都在做相同的事情时,为什么楼层除法运算符//要比普通除法运算符快/?
考虑这段代码
#include <bits/stdc++.h>
using namespace std;
struct foo {
template <typename T>
foo& operator<< (const T& var) {
cout << var;
return *this;
}
} bar;
int main() {
bar << 1 << '\n';
bar << 1.2 << '\n';
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想<<只为整数数据类型重载运算符.(int16_t,int32_t,int64_t)
我怎样才能做到这一点?