读取并解析 C/C++; 中的行将标记放入数组或向量或类似结构中

发布于 2024-08-22 02:02:13 字数 385 浏览 8 评论 0原文

我必须针对 ACM IPC 中的一个问题提交代码,您可能知道,时间非常重要。所以,我必须有效地读取这样的输入: 第一行将包含关联的整数值序列,第二行将包含与另一个序列关联的整数值序列。 例如:

3 2 1 4 5 7 6    
3 1 2 5 6 7 4   
7 8 11 3 5 16 12 18   
8 3 11 7 16 18 12 5   
255  
255

我必须将第一行放在一个数组中,将第二行放在另一个数组中,并将两者传递到一个函数中。

我如何读取这些内容并将其放入 C/C++ 中?我正在以 C 方式思考,但我的方法将有 2 while 的... 我偏好使用 scanf 进行读取,但解析可以按照您的意愿进行。

请帮助这个新手!

I have to submit code to one of the problems in ACM IPC and, as you may know, the time counts a lot. So, I have to read efficiently an input like this:
The first line will contain the sequence of integer values associated and the second line will contain the sequence of integer values associated with another sequence.
E.g.:

3 2 1 4 5 7 6    
3 1 2 5 6 7 4   
7 8 11 3 5 16 12 18   
8 3 11 7 16 18 12 5   
255  
255

I have to put the 1st line in an array and the second in another and pass both in a function.

How do I read and put these in C/C++? I was thinking in a C way, but my approach would have 2 while's...
I have preference reading with scanf, but the parsing can be done as you want.

Please , help this newb!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

滴情不沾 2024-08-29 02:02:13

使用 std::getline()< 读取这些行/a>.然后使用 std::stringstream 来解析每行。由于这是一场竞赛,因此您不需要实际的代码。

Read the lines using std::getline(). Then use a std::stringstream to parse each line. As this is for a competition, you won't be wanting actual code.

别念他 2024-08-29 02:02:13
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>

typedef std::vector< int > ints_t;

void dump_ints( const ints_t& input )
{
    std::copy(
        input.begin(),
        input.end(),
        std::ostream_iterator< int >( std::cout, " " ) );
    std::cout << std::endl;
}

void foo( const ints_t& first, const ints_t& second )
{
    dump_ints( first );
    dump_ints( second );
}

bool parse_line( std::istream& is, ints_t* output )
{
    std::string line;
    if ( std::getline( is, line ) )
    {
        std::istringstream raw_ints( line );
        std::copy(
            std::istream_iterator< int >( raw_ints ),
            std::istream_iterator< int >(),
            std::back_inserter( *output ) );
        return true;
    }
    else
    {
        return false;
    }
}

bool parse( std::istream& is, ints_t* first, ints_t* second )
{
    const bool result = parse_line( is, first ) && parse_line( is, second );
    return result;
}

void run( std::istream& is )
{
    while ( is )
    {
        ints_t first;
        ints_t second;
        if ( parse( is, &first, &second ) )
        {
            foo( first, second );
        }
    }
}

int main()
{
    //if you want to read input from file use ifstream and comment istringstream 
//    std::ifstream is( "put_here_a_path_to_input_file" );
    std::istringstream is( 
        "3 2 1 4 5 7 6\n"
        "3 1 2 5 6 7 4\n"
        "7 8 11 3 5 16 12 18\n"
        "8 3 11 7 16 18 12 5\n"
        "255\n"
        "255\n" 
        );
    run( is );
}
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>

typedef std::vector< int > ints_t;

void dump_ints( const ints_t& input )
{
    std::copy(
        input.begin(),
        input.end(),
        std::ostream_iterator< int >( std::cout, " " ) );
    std::cout << std::endl;
}

void foo( const ints_t& first, const ints_t& second )
{
    dump_ints( first );
    dump_ints( second );
}

bool parse_line( std::istream& is, ints_t* output )
{
    std::string line;
    if ( std::getline( is, line ) )
    {
        std::istringstream raw_ints( line );
        std::copy(
            std::istream_iterator< int >( raw_ints ),
            std::istream_iterator< int >(),
            std::back_inserter( *output ) );
        return true;
    }
    else
    {
        return false;
    }
}

bool parse( std::istream& is, ints_t* first, ints_t* second )
{
    const bool result = parse_line( is, first ) && parse_line( is, second );
    return result;
}

void run( std::istream& is )
{
    while ( is )
    {
        ints_t first;
        ints_t second;
        if ( parse( is, &first, &second ) )
        {
            foo( first, second );
        }
    }
}

int main()
{
    //if you want to read input from file use ifstream and comment istringstream 
//    std::ifstream is( "put_here_a_path_to_input_file" );
    std::istringstream is( 
        "3 2 1 4 5 7 6\n"
        "3 1 2 5 6 7 4\n"
        "7 8 11 3 5 16 12 18\n"
        "8 3 11 7 16 18 12 5\n"
        "255\n"
        "255\n" 
        );
    run( is );
}
旧梦荧光笔 2024-08-29 02:02:13

您还可以使用 strtok() 和 strdup()。

请参阅使用 strtok() 的示例strdup()。然后将使用 strtok() 提取各个标记 - strdup() 分配空间并复制它们。

You could also use strtok() and strdup().

See example of using strtok() and strdup(). strtok() would then be used to extract the individual tokens - strdup() to allocate space an copy them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文