Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;tokeniser,跳过以'//';以及特设'/**/'; 我正在练习用C++编写一个记录器,但是,我假设我需要把一些注释作为输入文件。那我怎么能跳过下面图片中的评论呢。 有什么想法吗?_C++ - Fatal编程技术网

C++;tokeniser,跳过以'//';以及特设'/**/'; 我正在练习用C++编写一个记录器,但是,我假设我需要把一些注释作为输入文件。那我怎么能跳过下面图片中的评论呢。 有什么想法吗?

C++;tokeniser,跳过以'//';以及特设'/**/'; 我正在练习用C++编写一个记录器,但是,我假设我需要把一些注释作为输入文件。那我怎么能跳过下面图片中的评论呢。 有什么想法吗?,c++,C++,对于/使用regex库,您可以: // for "//" for( std::string line; std::getline( input_file_stream, line ); ){ if( std::regex_search( line, std::regex( R"(^//)" ) ) ){ // nothing } else { std::cout << line << '\n'; }

对于
/
使用
regex
库,您可以:

// for "//"
for( std::string line; std::getline( input_file_stream, line ); ){
    if( std::regex_search( line, std::regex( R"(^//)" ) ) ){
            // nothing
    }
    else {
        std::cout << line << '\n';
    }
}
std::ifstream input_file_stream( "file" );

// for // and /* up to */
for( std::string line; std::getline( input_file_stream, line ); ){
    if( *line.begin() == '/' ){
        if( *(line.begin() + 1) == '*' ){
            while( *line.begin() != '*' && *(line.begin() + 1 ) != '/' ){
                std::getline( input_file_stream, line );
            }
        }
    } else {
        std::cout << line << '\n';
    }
}

input_file_stream.close();  
测试,输入:

// skip
// skip
okay
// skip
okay
okay
/* skip up to
not
not
not
*/
okay
the end  
okay
okay
okay
okay
the end
和输出:

// skip
// skip
okay
// skip
okay
okay
/* skip up to
not
not
not
*/
okay
the end  
okay
okay
okay
okay
the end

起初我尝试了一种方法,比如,读取第一个标记字符,如果是“/”,则继续读取下一个字符,直到行尾。但它只适用于第一行。您会接受基于sed的解决方案吗?或者如果您坚持使用awk、perl等?您想让我们编写代码?请通过编辑您的问题提供更多信息。啊,是的,您是对的@EdHeal。你好,欢迎来到StackOverflow。请参加stackoverflow.com/tour之旅,学习提出好问题stackoverflow.com/help/how-to-ask,制作一个MCVE stackoverflow.com/help/MCVE