gpt4 book ai didi

c++ - 不理解这段代码(无条件运行)

转载 作者:可可西里 更新时间:2023-11-01 16:56:59 25 4
gpt4 key购买 nike

我正在学习 C++,但在我读过的任何一本书中都没有真正看到这一点。我想阅读和评论代码,以便我可以更好地学习,并遇到了一段奇怪的代码,它运行但没有条件。根据我阅读的内容(以及我使用其他语言的经验,您需要一个 if、while、for 或 something for blocks)。

我正在查看 tbb 线程包,所以我不确定它是否与启动线程或特定于 c++ 相关(如果您不认为这是 c++ 中常见的东西,那么它可能特定于 tdd)。

我想我明白里面的代码实际上做了什么,但我不确定它是如何被触发或运行的。有任何想法吗?

这是部分:

    {
//this is the graph part of the code
Graph g;
g.create_random_dag(nodes);
std::vector<Cell*> root_set;
g.get_root_set(root_set);
root_set_size = root_set.size();
for( unsigned int trial=0; trial<traversals; ++trial ) {
ParallelPreorderTraversal(root_set);
}
}

附注如果对您有帮助,这里是整个文件(上面的代码在 main() 的中间)。

#include <cstdlib>
#include "tbb/task_scheduler_init.h"
#include "tbb/tick_count.h"
#include "../../common/utility/utility.h"
#include <iostream>
#include <vector>
#include "Graph.h"

// some forward declarations
class Cell;
void ParallelPreorderTraversal( const std::vector<Cell*>& root_set );

//------------------------------------------------------------------------
// Test driver
//------------------------------------------------------------------------
utility::thread_number_range threads(tbb::task_scheduler_init::default_num_threads);
static unsigned nodes = 1000;
static unsigned traversals = 500;
static bool SilentFlag = false;

//! Parse the command line.
static void ParseCommandLine( int argc, const char* argv[] ) {
utility::parse_cli_arguments(
argc,argv,
utility::cli_argument_pack()
//"-h" option for for displaying help is present implicitly
.positional_arg(threads,"n-of-threads","number of threads to use; a range of the form low[:high], where low and optional high are non-negative integers or 'auto' for the TBB default.")
.positional_arg(nodes,"n-of-nodes","number of nodes in the graph.")
.positional_arg(traversals,"n-of-traversals","number of times to evaluate the graph. Reduce it (e.g. to 100) to shorten example run time\n")
.arg(SilentFlag,"silent","no output except elapsed time ")
);
}

int main( int argc, const char* argv[] ) {
try {
tbb::tick_count main_start = tbb::tick_count::now(); //tbb counter start
ParseCommandLine(argc,argv);

// Start scheduler with given number of threads.
std::cout << threads << std::endl;
for( int p=threads.first; p<=threads.last; ++p ) {
tbb::tick_count t0 = tbb::tick_count::now(); //timer
tbb::task_scheduler_init init(4); //creates P number of threads
srand(2); //generates a random number between 0-2?
size_t root_set_size = 0;
{
//this is the graph part of the code
Graph g;
g.create_random_dag(nodes);
std::vector<Cell*> root_set;
g.get_root_set(root_set);
root_set_size = root_set.size();
for( unsigned int trial=0; trial<traversals; ++trial ) {
ParallelPreorderTraversal(root_set);
}
}
tbb::tick_count::interval_t interval = tbb::tick_count::now()-t0; //counter done
if (!SilentFlag){ //output the results
std::cout
<<interval.seconds()<<" seconds using "<<p<<" threads ("<<root_set_size<<" nodes in root_set)\n";
}
}
utility::report_elapsed_time((tbb::tick_count::now()-main_start).seconds());

return 0;
}catch(std::exception& e){
std::cerr
<< "unexpected error occurred. \n"
<< "error description: "<<e.what()<<std::endl;
return -1;
}
}

最佳答案

不,您不需要 ifwhile 语句来引入新级别的范围。基本上 { 符号打开一个新的范围级别,} 结束它。通常的作用域规则适用,例如,在这个新 block 中定义的变量在外部未定义,在 block 结束时运行对象析构函数,并且在上面的作用域级别中与另一个名称相同的变量将被隐藏。

一个常见的用例是在 switch 语句中。例如,

switch (a)
{
case 1:
{
int i;
}
case 2:
{
int i; //note reuse of variable with the same name as in case 1
}
}

如果 case 语句中没有 { },编译器将提示多重定义的标识符。

关于c++ - 不理解这段代码(无条件运行),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10593140/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com