简体   繁体   中英

Store semicolon-separated integers into array after reading from file

I have my data from a text file that looks like this:

...
[timestamp]
1096757
[logtime]
2019-10-22 15:29:17.435
[scan]
32;39;36;39;39;39;39;42;42;44;46;48;48;49;51;52;54;54;54;53;50;50;50;54;54;56;56;56;56;57;57;56;56;58;56;56;57;58;58;59;58;59;60;61;60;61;61;60;60;61;61;62;63;63;63;63;63;61;61;61;61;61;61;62;62;62;62;62;62;64;68;64;64;63;61;61;61;60;61;60;62;60;60;59;60;59;59;57;62;57;57;57;57;57;58;63;63;63;63;63;61;61;62;59;59;61;62;61;63;64;63;61;63;61;61;61;63;63;63;59;62;62;59;60;60;60;57;60;61;61;61;61;61;61;56;56;56;57;59;61;61;59;57;57;57;57;59;61;59;61;60;60;60;63;63;63;63;63;63;61;61;61;62;62;62;62;65;69;69;68;68;70;68;72;72;72;71;72;72;73;72;73;73;73;73;73;73;73;73;72;71;69;69;69;69;70;73;70;68;68;68;66;65;66;66;64;64;64;62;60;57;57;57;53;53;53;51;50;47;46;45;44;44;44;44;43;42;43;43;44;45;45;45;45;
[timestamp]
1096858
[logtime]
2019-10-22 15:29:17.532
[scan]
41;38;38;38;38;38;40;40;41;42;46;47;49;49;48;49;50;49;51;52;52;52;52;53;53;53;57;58;58;58;58;57;57;57;56;56;59;56;57;58;58;58;58;58;61;63;59;60;61;60;60;61;61;61;61;61;61;62;62;60;60;60;60;60;60;61;61;61;62;63;63;62;62;62;62;64;65;65;64;59;59;59;60;60;61;61;60;63;63;62;59;58;58;58;59;61;61;62;64;64;64;63;63;60;60;63;64;64;64;63;63;60;58;58;58;59;59;59;59;59;59;59;61;60;60;60;60;60;60;60;60;61;61;59;59;58;58;59;62;62;62;59;59;59;59;59;61;61;61;61;61;60;60;62;62;62;62;62;62;62;62;64;64;66;66;69;69;70;69;69;70;71;71;72;72;72;72;72;73;74;74;71;71;71;70;71;71;71;71;72;72;76;72;71;70;71;70;71;70;66;65;65;64;63;64;60;60;60;
[timestamp]
...

I am trying to take the first 10 numbers from the data after the word [scan] . I managed to make my code find the line number for the data, but I couldn't find a way to take the data (the first 10 of every scan) into an array of int.

I used some code that I found, but it didn't work as I expected.

I am trying to save the data to look like this. The first array contains:

32
39
36
39
39
39
39
42
42
44

The second array contains this:

41
38
38
38
38
38
40
40
41
42

As you wrote in your question your are able to find the lines which contains desired data in your input file. So i assume you are able to read that line and store it as std::string . The following example shows how to extract the first 10 numbers from std::string separated by semicolon and add it to a std::vector<int> .

#include <iostream>
#include <string>
#include <vector>

int main()
{
    std::string str = "32;39;36;39;39;39;39;42;42;44;46;48;48;49;51;52;54;";
    std::vector<int> token_list;
    std::string::size_type start_pos = 0;
    std::string::size_type pos = 0;

    while ((token_list.size() < 10) && ((pos = str.find(";", start_pos)) != std::string::npos))
    {
        token_list.push_back(std::stoi(str.substr(start_pos, pos - start_pos)));
        // Set next start position for search behind found semicolon
        start_pos = pos+1;
    }

    // Print extracted data
    for (size_t i = 0; i < token_list.size(); i++)
    {
        std::cout << token_list[i] << std::endl;
    }

    return 0;
}

If you want to split a string into substrings using a special pattern, then this is called tokenizing. This is a very old mechanism and even in C there was a function strtok to split a string into tokens.

In C++ there is also an algorithm available, especially in cooperation with container classes.

This is the so called std::sregex_token_iterator . See here . This iterates over a std::string and returns the tokens.

So, you have a specific and dedicated functionality that you should use.

The next dedicated function for copying n data from one container to another container is std::copy_n .

With that, the whole task will be done with a one liner.

Also transformation from the string-data to any number format can be done by the dedicated function std::transfrom .

And the output is also copied from the vector to std::cout .

Please see the below simple example:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
#include <regex>

int main() {

    // The source values
    std::string source{ "32;39;36;39;39;39;39;42;42;44;46;48;48;49;51;52;54;" };

    // Here we will store the result
    std::vector<std::string> destination{}; // As string
    std::vector<int> data{};                // Int, or whatever else you want

    // This is the separator betwenn the numbers
    std::regex separator{ ";" };

    // One-liner to copy 10 sub strings to the destination array
    std::copy_n(std::sregex_token_iterator(source.begin(), source.end(), separator, -1), 10, std::back_inserter(destination));

    // Convert ot ints (or whatever)
    std::transform(destination.begin(), destination.end(), std::back_inserter(data), [](const std::string& s) { return std::stoi(s); });

    // And show result on std::cout
    std::copy(data.begin(), data.end(), std::ostream_iterator<int>(std::cout, "\n"));

    return 0;
}

Please note: You could of course directly copy the destination data to std::cout . The user will not see the difference on the screen. But if you want to do some further calculations, then you need the transformation.

And, also std::transform can directly write to std::cout . Like below:

    std::transform(destination.begin(), destination.end(), std::ostream_iterator<int>(std::cout, "\n"), [](const std::string & s) { return std::stoi(s); });

Please notice the beauty of modern C++ algorithms. There is more to come with C++20.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM