小编New*_*ech的帖子

如何最好地访问 std::pair 的元素?

在使用 std::pair 时,我遇到了两种不同的方法来访问其元素。由于它们似乎都有效且有效,我想知道它们之间有什么区别,哪种方法是首选?

std::pair<int, int> p(1,1); // can be of any type.
int i1 = p.first;        // first approach
int i2 = std::get<0>(p); // second approach
Run Code Online (Sandbox Code Playgroud)

c++ std-pair

6
推荐指数
1
解决办法
4691
查看次数

具有相同索引的for循环的性能

在编码时我遇到了一个问题:

当我必须使用大量for循环时,所有迭代都在不同的范围内.如果我只将一个变量声明为索引(示例I)或者它根本不重要(示例II),那么性能(即运行时)会更好吗?

例I:

int ind;
for(ind=0; ind < a; ind++) { /*do something*/ }
for(ind=0; ind < b; ind++) { /*to something*/ }
...
for(ind=0; ind < z; ind++) { /*to something*/ }
Run Code Online (Sandbox Code Playgroud)

例二:

for(int ind=0; ind < a; ind++) { /*do something*/ }
...
for(int ind=0; ind < z; ind++) { /*do something*/ }
Run Code Online (Sandbox Code Playgroud)

谢谢您的帮助

c c++ indexing performance for-loop

3
推荐指数
1
解决办法
192
查看次数

标签 统计

c++ ×2

c ×1

for-loop ×1

indexing ×1

performance ×1

std-pair ×1