I wanted to compare reading lines of string input from stdin using Python and C++ and was shocked to see my C++ code run an order of magnitude slower than the equivalent Python code. Since my C++ is rusty and I'm not yet an expert Pythonista, please tell me if I'm doing something wrong or if I'm misunderstanding something. (TLDR answer: include the statement: cin.sync_with_stdio(fa
我想比较使用Python和C ++的stdin字符串输入的读取行,并且很惊讶地看到我的C ++代码比等效的Python代码慢了一个数量级。 由于我的C ++很生疏,而且我还不是专家蟒蛇,所以请告诉我,如果我做错了什么或者我误解了某些东西。 (TLDR答案:包括声明: cin.sync_with_stdio(false)或者只是使用fgets代替。 TLDR结果:一直滚动到我的问题的底部并查看表格。) C ++代码: #include <iostream> #include <time.h>
Say I have a function whose prototype looks like this, belonging to class container_class : std::vector<int> container_class::func(int param); The function may or may not cause an infinite loop on certain inputs; it is impossible to tell which inputs will cause a success and which will cause an infinite loop. The function is in a library of which I do not have the source of and cannot m
假设我有一个函数,它的原型看起来像这样,属于类container_class : std::vector<int> container_class::func(int param); 该函数可能会或可能不会导致某些输入的无限循环; 不可能知道哪些输入会导致成功,哪些会导致无限循环。 该函数位于我没有源代码也无法修改的库中(这是一个错误,将在几个月后的下一个版本中修复,但现在我需要一种方法来解决它),所以修改功能或类别的解决方案将不起作用。 我试过使用std:
I've been told by others that writing using namespace std in code is wrong, and that I should use std::cout and std::cin directly instead. Why is using namespace std considered a bad practice? Is it inefficient or does it risk declaring ambiguous variables (variables that share the same name as a function in std namespace)? Does it impact performance? This is not related to performance
其他人告诉我,在代码中using namespace std编写是错误的,我应该直接使用std::cout和std::cin 。 为什么using namespace std被认为是一个不好的做法? 它效率低下还是会冒险声明不明确的变量(与std命名空间中的函数共享相同名称的变量)? 它会影响性能吗? 这根本与性能无关。 但考虑一下:你正在使用两个名为Foo和Bar的库: using namespace foo; using namespace bar; 一切正常,你可以从Foo和Quux()从Bar中调用Bl
你如何在C / C ++中设置,清除和切换一点? Setting a bit Use the bitwise OR operator ( | ) to set a bit. number |= 1UL << x; That will set bit x . Use 1ULL if number is wider than unsigned long ; promotion of 1UL << x doesn't happen until after evaluating 1UL << x where it's undefined behaviour to shift by more than the width of a long . The same applies to all the
你如何在C / C ++中设置,清除和切换一点? 设置一下 使用位运算符( | )设置一个位。 number |= 1UL << x; 这将设置位x 。 如果number宽于unsigned long则使用1ULL ; 在评估1UL << x之后, 1UL << x推广1UL << x发生,其中它的未定义行为的移动幅度超过了一个long的宽度。 这同样适用于所有其他例子。 清理一下 使用按位AND运算符( & )清除一点。 number &= ~(1UL << x);
What are the proper uses of: static_cast dynamic_cast const_cast reinterpret_cast C-style cast (type)value Function-style cast type(value) How does one decide which to use in which specific cases? static_cast is the first cast you should attempt to use. It does things like implicit conversions between types (such as int to float , or pointer to void* ), and it can also call expli
什么是正确的用途: static_cast dynamic_cast const_cast reinterpret_cast C风格演员(type)value 函数式投射type(value) 如何决定在哪些特定情况下使用哪一种? static_cast是您应该尝试使用的第一个演员。 它执行诸如类型之间的隐式转换(例如int到float或void* ),它也可以调用显式转换函数(或隐式转换函数)。 在许多情况下,明确指出static_cast不是必需的,但需要注意的是T(something)语法等同于(T)s
C ++中explicit关键字的含义是什么? The compiler is allowed to make one implicit conversion to resolve the parameters to a function. What this means is that the compiler can use constructors callable with a single parameter to convert from one type to another in order to get the right type for a parameter. Here's an example class with a constructor that can be used for implicit conversions
C ++中explicit关键字的含义是什么? 允许编译器进行一次隐式转换以将参数解析为一个函数。 这意味着编译器可以使用可用单个参数调用的构造函数将一种类型转换为另一种类型,以便为参数获取正确的类型。 下面是一个带有可用于隐式转换的构造函数的示例类: class Foo { public: // single parameter constructor, can be used as an implicit conversion Foo (int foo) : m_foo (foo) { } int GetFoo () { retur
What are the benefits of passing by pointer over passing by reference in C++? Lately, I have seen a number of examples that chose passing function arguments by pointers instead of passing by reference. Are there benefits to doing this? Example: func(SPRITE *x); with a call of func(&mySprite); vs. func(SPRITE &x); with a call of func(mySprite); A pointer can receive a NULL par
将指针传递给C ++传递参考的好处是什么? 最近,我看到许多例子都是通过指针来选择传递函数参数,而不是通过引用传递。 这样做有好处吗? 例: func(SPRITE *x); 打电话给 func(&mySprite); 与 func(SPRITE &x); 打电话给 func(mySprite); 一个指针可以接收一个NULL参数,一个参考参数不能。 如果有可能想要传递“无对象”,则使用指针而不是引用。 另外,通过指针传递允许您在调用站点显式查看该对象是按
What is the most elegant way to iterate the words of a string? The string can be assumed to be composed of words separated by whitespace. Note that I'm not interested in C string functions or that kind of character manipulation/access. Also, please give precedence to elegance over efficiency in your answer. The best solution I have right now is: #include <iostream> #include <s
什么是迭代字符串的最优雅的方式? 该字符串可以被认为是由用空格分隔的单词组成的。 请注意,我对C字符串函数或那种字符操作/访问不感兴趣。 另外,请在回答中优先考虑优雅而不是效率。 我现在最好的解决方案是: #include <iostream> #include <sstream> #include <string> using namespace std; int main() { string s = "Somewhere down the road"; istringstream iss(s); do {
I recently acquired a computer with dual-boot to code in C++. On windows I use intel C++ compiler and g++ on linux. My programs consist mostly of computation (fixed point iteration algorithm with numerical integration, etc.). I thought I could get performances close to windows on my linux, but so far I don't: for the exact same code, the program compiled with g++ is about 2 times slower t
我最近购买了一台双启动电脑,以C ++编写代码。 在Windows上,我在Linux上使用intel C ++编译器和g ++。 我的程序主要由计算(数值积分的固定点迭代算法等)组成。 我认为我可以在我的linux上获得接近windows的性能,但到目前为止我没有:对于完全相同的代码,使用g ++编译的程序比使用intel编译器的程序慢大约2倍。 据我所知,icc可能会更快,甚至可能高达20-30%的收益,但我没有读到任何有关它两倍的速度(一般来说,我
Here is the extract from the program in question. The matrix img[][] has the size SIZE×SIZE, and is initialized at: img[j][i] = 2 * j + i Then, you make a matrix res[][] , and each field in here is made to be the average of the 9 fields around it in the img matrix. The border is left at 0 for simplicity. for(i=1;i<SIZE-1;i++) for(j=1;j<SIZE-1;j++) { res[j][i]=0;
这是来自该程序的摘录。 矩阵img[][]的大小为SIZE×SIZE,初始化为: img[j][i] = 2 * j + i 然后,你创建一个矩阵res[][] ,并且这里的每个字段都是img矩阵中围绕它的9个字段的平均值。 为简单起见,边框保留为0。 for(i=1;i<SIZE-1;i++) for(j=1;j<SIZE-1;j++) { res[j][i]=0; for(k=-1;k<2;k++) for(l=-1;l<2;l++) res[j][i] += img[j+l][i+k];