考虑以下两个例子.
class ClassOne
{
//class definition is here
};
std::vector< ClassOne > myListOfObjects;
std::vector< ClassOne >::const_iterator iter = myListOfObjects.begin();
Example 1:
for( ; iter < myListOfObjects.end(); **++iter**)
{
//some operations
}
OR
Example 2:
for( ; iter < myListOfObjects.end(); **iter++**)
{
//some operations
}
Run Code Online (Sandbox Code Playgroud)
哪一个更快?循环上下文中的++ iter或iter ++.
从Brian的帖子复制(使问题更简洁).
我看到有人使用这种方法来增加变量:
r = (r + 1) & 0xf;
Run Code Online (Sandbox Code Playgroud)
这种方法比仅使用更好/更快:
r++;
Run Code Online (Sandbox Code Playgroud)
为什么有人会使用按位,如果只是重复,则使用0xf?
我想我不完全理解析构函数在C++中是如何工作的.这是我为重新创建问题而编写的示例程序:
#include <iostream>
#include <memory>
#include <vector>
using namespace std;
struct Odp
{
int id;
Odp(int id)
{
this->id = id;
}
~Odp()
{
cout << "Destructing Odp " << id << endl;
}
};
typedef vector<shared_ptr<Odp>> OdpVec;
bool findOdpWithID(int id, shared_ptr<Odp> shpoutOdp, OdpVec& vec)
{
shpoutOdp.reset();
for (OdpVec::iterator iter = vec.begin(); iter < vec.end(); iter++)
{
Odp& odp = *(iter->get());
if (odp.id == id)
{
shpoutOdp.reset(iter->get());
return true;
}
}
return false;
}
int main()
{
OdpVec vec;
vec.push_back(shared_ptr<Odp>(new …Run Code Online (Sandbox Code Playgroud) 在C++,我看到人们经常++p在for循环或其他地方使用,你想要增加一个值,但不使用返回值.我听说这更有效,因为在递增之前p++返回值,因此需要一个临时空间.
但是,感觉即使是非常无知的编译器也会将返回值作为死代码拉出(只要增量代码是内联的,因此编译器可以看到返回值不是必需的).
我试图想出一个很好的例子,使用某种迭代器,iter++实际上会创建副本(即使iter++没有使用返回值).
毕竟,当我们用迭代器编写代码时,我们并不经常考虑寄存器分配.
我学会了使用,p++因为这就是我从中学到的那本书.更喜欢++p什么时候没有使用回归价值的古老做法,或者只是优雅编码的一部分?为什么语言不被称为++C呢?
最近我参加了一次采访,有几个问题,我必须找到代码中的缺陷.以下是问题.
void fun(char *p)
{
int a = 0;
int b = strlen(p) - 1;
int d = 0;
while(d == 0)
{
if(a == b)
{
d = 1;
}
else
{
char t = *(p + a);
*(p + a) = *(p + b);
*(p + b) = t;
a += 1;
b -= 1;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的回答是:
NULL检查p. if(a == b).如果字符串的长度是偶数,则这种情况永远不会满足.如果有人发现任何其他缺陷,请告诉我,并且欢迎任何对我给出的答案的评论.
我想做一个for循环,创建更多的线程.
我尝试过:
int i;
for (i = 0; i < 10; i++) {
thread t1(nThre);
t1.join();
cout << "Joined thread n'" << i << '\n';
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用.nThre顺序调用(这是一个简单的void例程).
我还询问我是否可以使用预增的i只是一个int,所以:
++iinsted的的i++,这应该是更好的性能.
我听到一位教授说“避免在上下文允许选择前缀的后缀运算符”。我进行搜索,但在stackoverflow中找不到相关的帖子来解释这一点。
当我们有能力选择操作员++时,为什么更喜欢使用prefixoperator ++ postfix?
例如..如果我有.
#include <iostream>
using namespace std;
int main()
{
int counter = 0;
while (true)
{
cout << counter << endl;
counter++
}
}
Run Code Online (Sandbox Code Playgroud)
并且说我在与其他计算机竞争10亿的竞争中,这个循环的运行速率纯粹取决于计算机处理器的速度?或者我的程序运行速度有限制,这可能是多变的?
今天我写了一些有趣的代码,将整数转换为罗马数字.整个运行代码在这里:
#include <iostream>
#include <map>
#include <string>
using namespace std;
string arabic2roman(int i){
//if(i==0) return "ZERO";
map<int, string> m;
m.insert(pair<int,string>(0,"ZERO"));
m.insert(pair<int,string>(1,"I"));
m.insert(pair<int,string>(4,"IV"));
m.insert(pair<int,string>(5,"V"));
m.insert(pair<int,string>(9,"IX"));
m.insert(pair<int,string>(10,"X"));
m.insert(pair<int,string>(40,"XL"));
m.insert(pair<int,string>(50,"L"));
m.insert(pair<int,string>(90,"XC"));
m.insert(pair<int,string>(100,"C"));
m.insert(pair<int,string>(400,"CD"));
m.insert(pair<int,string>(500,"D"));
m.insert(pair<int,string>(900,"CM"));
m.insert(pair<int,string>(1000,"M"));
string roman;
map<int,string>::iterator iter;
for(iter=m.end();iter !=m.begin();iter--){
while(i >=iter->first){
roman+=iter->second;
i-=iter->first;
}
}
return roman;
}
int main(){
int test=12345;
cout << arabic2roman(test) << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这段代码现在在我的Xcode 4.6.2上工作正常.但是如果在if(i == 0)之前的第8行中删除"//",则返回"ZERO",在Xcode 4.6.2上,程序无休止地运行.任何人都能解释一下吗?谢谢!
我一直认为,必须相反.但当我尝试这个简单的代码时,我得到了意想不到的结果:
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <chrono>
using namespace std;
int main(int argc, char* argv[])
{
int x = 0, y = 0;
double z;
chrono::steady_clock::time_point start_point;
start_point = chrono::steady_clock::now();
for(int i = 0; i < 100000; x = ++i)
for(int j = 0; j < 100000; y = ++j)
z = static_cast<double>(x * y);
cout << "The prefix increment took a " << chrono::duration_cast<chrono::milliseconds>(chrono::steady_clock::now() - start_point).count() << " milliseconds" << endl;
start_point = chrono::steady_clock::now();
for(int i = …Run Code Online (Sandbox Code Playgroud) c++ ×9
performance ×2
c ×1
destructor ×1
for-loop ×1
if-statement ×1
iostream ×1
loops ×1
operators ×1
return ×1