在抛出std :: out_of_range;错误?

发布于 2025-01-22 14:58:09 字数 1022 浏览 3 评论 0原文

我尝试阅读一些CSV数据,但是我一直遇到此错误:

丢弃'std :: out_of_range'
的实例终止 what():vector :: _ m_range_check:__n(是1)> = this-> size() (是1)

customer.csv

id, name, address

1, "Knut", "Knutveien 3"

2, "Lise", "Liseveien 7"

ma​​in.cpp

#include <iostream>
#include "rapidcsv/rapidcsv.h"

using namespace std;

void read_customer()
{
    rapidcsv::SeparatorParams sp; // make a object sp
    sp.mTrim = true; // remove the line space, equal to true
    // open the customer document
    rapidcsv::Document doc_customer("customers.csv", rapidcsv::LabelParams(), sp);
    for (int i = 0; i < doc_customer.GetRowCount(); i++)
    {
        auto name = doc_customer.GetCell<string>("name", i);
        auto address = doc_customer.GetCell<string>("address", i);
        cout << "customer: " << name << ", " << address << endl;
    }
}

int main()
{
    read_customer();
    return 0;
}

I try to read some csv data, but I keep getting this error:

terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check: __n (which is 1) >= this->size()
(which is 1)

customer.csv

id, name, address

1, "Knut", "Knutveien 3"

2, "Lise", "Liseveien 7"

main.cpp

#include <iostream>
#include "rapidcsv/rapidcsv.h"

using namespace std;

void read_customer()
{
    rapidcsv::SeparatorParams sp; // make a object sp
    sp.mTrim = true; // remove the line space, equal to true
    // open the customer document
    rapidcsv::Document doc_customer("customers.csv", rapidcsv::LabelParams(), sp);
    for (int i = 0; i < doc_customer.GetRowCount(); i++)
    {
        auto name = doc_customer.GetCell<string>("name", i);
        auto address = doc_customer.GetCell<string>("address", i);
        cout << "customer: " << name << ", " << address << endl;
    }
}

int main()
{
    read_customer();
    return 0;
}

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

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

发布评论

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

评论(1

孤千羽 2025-01-29 14:58:09

输入数据看起来不像常规CSV数据。如果您从数据中删除空白行,然后将其更改为类似的内容:

id, name, address
1, "Knut", "Knutveien 3"
2, "Lise", "Liseveien 7"

在尝试阅读它之前,appapcsv能够正确解析它。

除了使用不同的文件名customers.csv外,C ++代码看起来还不错。

为了完整性,我从customer.csv中删除了空白行,并使用了以下main.cpp contents可靠地解析文件。

#include <iostream>
#include <vector>
#include "rapidcsv.h"

void read_customer()
{
  rapidcsv:: SeparatorParams sp; // make a object sp
  sp.mTrim = true;  //remove the line space, equal to true

  // open the customer document
  rapidcsv:: Document doc_customer("customer.csv", rapidcsv::LabelParams(), sp);

  for (int i = 0; i < doc_customer.GetRowCount(); i++)
  {
    auto name =  doc_customer.GetCell<std::string>("name", i);
    auto address = doc_customer.GetCell<std::string>("address", i);
    std::cout << "customer: " << name << ", " << address << std::endl;
  }
}

int main()
{
  read_customer();  
  return 0;
}

The input data does not look like regular CSV data. If you remove the blank lines from the data, and change it into to something like:

id, name, address
1, "Knut", "Knutveien 3"
2, "Lise", "Liseveien 7"

before trying to read it, rapidcsv is able to parse it correctly.

The C++ code looks OK aside from using different filename customers.csv than specified in the question (customer.csv).

For completeness, I removed the blank lines from customer.csv and used the following main.cpp content which succefully parsed the file.

#include <iostream>
#include <vector>
#include "rapidcsv.h"

void read_customer()
{
  rapidcsv:: SeparatorParams sp; // make a object sp
  sp.mTrim = true;  //remove the line space, equal to true

  // open the customer document
  rapidcsv:: Document doc_customer("customer.csv", rapidcsv::LabelParams(), sp);

  for (int i = 0; i < doc_customer.GetRowCount(); i++)
  {
    auto name =  doc_customer.GetCell<std::string>("name", i);
    auto address = doc_customer.GetCell<std::string>("address", i);
    std::cout << "customer: " << name << ", " << address << std::endl;
  }
}

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