【问题标题】:how can i save an unknow number integers from a .txt file into an array如何将 .txt 文件中的未知数字整数保存到数组中
【发布时间】:2014-10-28 18:06:23
【问题描述】:

我生成了一个 data.txt 文件,其中包含大量整数,分为两列。

如何将这些整数保存到数组中?

如果有帮助,您可以找到data.txt here。示例:

600000
523887 283708
231749 419866
293707 273512
296065 215334
233447 207124
264381 460210
374915 262848
449017 329022
374111 151212
2933 496970

我试过这个,但由于某种原因它不起作用..

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
    fstream input("data-for-union-find.txt");
    int i=0;
    float a;
    int size=0;
    size=i/2;
    while (input >> a)
    {i++;
     }

    int *x=new int[size];
    int *y=new int[size+1];

    for(int j=0;j<(size+1);j++)
    {
        input>>x[j];
        input>>y[j];
        cout<<x[j]<<" "<<y[j]<<"              "<<j<<endl;
    return 0;
    }
}

【问题讨论】:

  • push_backstd::vector&lt;int&gt; 是你的朋友
  • std::vector&lt;std::pair&lt;int,int&gt; myvector; std::copy(std::istream_iterator&lt;std::pair&lt;int,int&gt;&gt;(std::ifstream("data-for-union-find.txt")),std::istream_iterator&lt;std::pair&lt;int,int&gt;&gt;(),std::back_insert_iterator(myvector));
  • @MooingDuck 没有为std::pair 定义提取器,std::istream_iterator 采用非常量左值引用。
  • @0x499602D2:我为 back_inserter 函数使用了错误的名称。事实证明,提取器位比我预期的更难解决。 coliru.stacked-crooked.com/a/d16b8bd180180e61
  • @MooingDuck 我找到了关于该主题的相关帖子。 -- stackoverflow.com/questions/16548379/…

标签: c++


【解决方案1】:

要向数组添加更多元素,超出其容量,您必须:

  1. 分配一个新的更大的数组。
  2. 将所有元素从旧数组复制到新数组。
  3. 删除旧数组。
  4. 添加新元素。

更安全的解决方案是使用std::vectorpush_back 方法。

如果你有大量数据,你可能想声明一个大尺寸的std::vector,以减少重新分配的次数。

【讨论】:

  • 我也是新的 c++ 我不知道该怎么做
  • 你不知道怎么办?声明一个向量?调用 std::vector 的方法?使用new 声明一个数组?请澄清。
【解决方案2】:

我会直接写入单个容器以避免分配:

#include <deque>
#include <iostream>
#include <sstream>

template <typename Sequence>
inline int xvalue(const Sequence& data, std::size_t index) {
    return data[2*index];
}

template <typename Sequence>
inline int yvalue(const Sequence& data, std::size_t index) {
    return data[2*index + 1];
}

int main() {
    std::istringstream stream(""
        "600000\n"
        "523887 283708\n"
        "231749 419866\n"
        "293707 273512\n"
        "296065 215334\n"
        "233447 207124\n"
        "264381 460210\n"
        "374915 262848\n"
        "449017 329022\n"
        "374111 151212\n"
        "2933 496970\n");
    // Get rid of the suspicious first value:
    int suspicious;
    stream >> suspicious;
    // Use a sequence, which avoids allocations
    std::deque<int> data
    // Read x,y:
    int x;
    int y;
    while(stream >> x >> y) {
        data.push_back(x);
        data.push_back(y);
    }
    // Display:
    for(std::size_t i = 0; i < data.size() / 2; ++i)
        std::cout << "x = " << xvalue(data, i) << ", y = " << yvalue(data, i) << '\n';
    // If you need contiguous memory
    std::vector<int> contiguous(data.begin(), data.end());
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-30
    • 2018-12-15
    • 2021-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多