C++。如何将元素添加到文件对向量中?

发布于 2025-02-12 12:07:42 字数 343 浏览 1 评论 0原文

我试图将每条奇数读成对。首先和每条偶数行,并将其放入成对的矢量中。我想在矢量中有一个球员和他的整体。 这是我的代码:

while(getline(players, player) && players >> overall)
    {
        pick_players.push_back(make_pair(player, overall));
    }

不幸的是,它仅读取前两行,其余的向量输出只是零。 player是字符串,总体是整数,player是我的fstream文件名。

I'm trying to read every odd line into pair.first and every even line to pair.second and put it into a vector of pairs. I'd like to have a player and his overall in one pair in a vector.
Here's my code :

while(getline(players, player) && players >> overall)
    {
        pick_players.push_back(make_pair(player, overall));
    }

Unfortunately it reads only the first two lines and the rest of vector output are just zeros. player is a string and overall is a integer, players is my fstream file name.

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

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

发布评论

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

评论(1

走走停停 2025-02-19 12:07:42

交流std :: getline操作员>>用于阅读输入流是不是一个好主意。相反,您最好坚持使用std :: getline在这里两次。

这样的事情会做:

  • 循环输入流,直到您无法读取两行(为此使用std :: getline)。
  • 将每个读数推回std :: vector< std :: pair< std :: string,int>>
  • 如果整个线路上存在解析错误,只需继续循环。

[demo]

#include <charconv>  // from_chars
#include <fmt/core.h>
#include <fmt/ranges.h>
#include <sstream>   // istringstream
#include <system_error>  // errc
#include <utility>   // pair
#include <vector>

int main() {
    std::istringstream iss{
        "player1\n"
        "1\n"
        "player2\n"
        "2ddd\n"
        "player3\n"
        "3   \n"
    };
    std::vector<std::pair<std::string, int>> players{};
    for (std::string player{}, overall_str{};
         getline(iss, player) and getline(iss, overall_str);) {
        int overall{};
        auto [ptr, ec] = std::from_chars(
            overall_str.data(), overall_str.data() + overall_str.size(), overall);
        if (ec == std::errc{}) {
            players.push_back({player, overall});
        } else {
            fmt::print("Error parsing overall line: {}\n", overall_str);
            continue;
        }
    }
    fmt::print("{}\n", players);
}

// Outputs:
//
//   [("player1", 1), ("player2", 2), ("player3", 3)]

您可以通过:

  • 修剪输入字符串和
  • 检查<<<<<<<<代码> std :: from_chars 使用所有输入字符串转换为数字。

[demo]

#include <boost/algorithm/string.hpp>
...
        boost::algorithm::trim(overall_str);
...
        if (ec == std::errc{} and ptr == overall_str.data() + overall_str.size()) {

// Outputs:
//
//   Error parsing overall line: 2ddd
//   [("player1", 1), ("player3", 3)]

It's not a good idea to interleave std::getline and operator>> for reading an input stream. Instead, you'd better stick to using std::getline twice here.

Something like this would do:

  • Loop the input stream until you can't read two lines (use std::getline for this).
  • Push back each reading into a std::vector<std::pair<std::string, int>>.
  • If there's a parsing error on the overall line, just continue looping.

[Demo]

#include <charconv>  // from_chars
#include <fmt/core.h>
#include <fmt/ranges.h>
#include <sstream>   // istringstream
#include <system_error>  // errc
#include <utility>   // pair
#include <vector>

int main() {
    std::istringstream iss{
        "player1\n"
        "1\n"
        "player2\n"
        "2ddd\n"
        "player3\n"
        "3   \n"
    };
    std::vector<std::pair<std::string, int>> players{};
    for (std::string player{}, overall_str{};
         getline(iss, player) and getline(iss, overall_str);) {
        int overall{};
        auto [ptr, ec] = std::from_chars(
            overall_str.data(), overall_str.data() + overall_str.size(), overall);
        if (ec == std::errc{}) {
            players.push_back({player, overall});
        } else {
            fmt::print("Error parsing overall line: {}\n", overall_str);
            continue;
        }
    }
    fmt::print("{}\n", players);
}

// Outputs:
//
//   [("player1", 1), ("player2", 2), ("player3", 3)]

You can strengthen the parsing of the overall line by:

  • trimming the input string, and
  • checking std::from_chars used all of the input string to convert to a number.

[Demo]

#include <boost/algorithm/string.hpp>
...
        boost::algorithm::trim(overall_str);
...
        if (ec == std::errc{} and ptr == overall_str.data() + overall_str.size()) {

// Outputs:
//
//   Error parsing overall line: 2ddd
//   [("player1", 1), ("player3", 3)]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文