C++ 如何在c+中的2d数组中插入可变数量的数据+;
我有以下文件input.txt,其中包含以下值:C++ 如何在c+中的2d数组中插入可变数量的数据+;,c++,arrays,file,input,dynamic-memory-allocation,C++,Arrays,File,Input,Dynamic Memory Allocation,我有以下文件input.txt,其中包含以下值: 0 0 0 1 0 2 0 3 1 1 1 4 2 1 2 4 2 3 我想将这些值插入两个数组。我希望他们最后看起来像这样 0 0 1 2 3 1 1 4 2 1 4 3 第一个数组将包含id,第二个数组将包含元素。我已通过以下方式为这两个阵列动态分配内存: int n,m,i=0,tempNum,lines; int NumberOfIds=0; ifstream read("input2.txt"); while(read&
0 0
0 1
0 2
0 3
1 1
1 4
2 1
2 4
2 3
我想将这些值插入两个数组。我希望他们最后看起来像这样
0 0 1 2 3
1 1 4
2 1 4 3
第一个数组将包含id,第二个数组将包含元素。我已通过以下方式为这两个阵列动态分配内存:
int n,m,i=0,tempNum,lines;
int NumberOfIds=0;
ifstream read("input2.txt");
while(read>>n>>m){
if (i==0){
tempNum=n;
}
if (tempNum != n){
NumberOfIds++;
}
tempNum = n;
i++;
}
lines=i-1;
//printf("%d", j);
int** idElements = new int*[NumberOfIds];
int* ids = new int[NumberOfIds];
int* numberOfIdElements = new int[NumberOfIds];
// Rewinds file
read.clear();
read.seekg(0, ios::beg);
int counter = 0;
NumberOfIds=0;
while(read>>n>>m){
if (tempNum != n){
numberOfIdElements[NumberOfIds] = counter;
NumberOfIds++;
counter = 0;
}
tempNum = n;
counter++;
i++;
}
for(int k = 0; k < NumberOfIds; ++k)
idElements[k] = new int[numberOfIdElements[k]];
intn,m,i=0,tempNum,行;
int NumberOfIds=0;
ifstream读取(“input2.txt”);
while(读>>n>>m){
如果(i==0){
tempNum=n;
}
if(tempNum!=n){
NumberOfIds++;
}
tempNum=n;
i++;
}
直线=i-1;
//printf(“%d”,j);
int**idements=new int*[NumberOfIds];
int*ids=新的int[NumberOfIds];
int*numberofidements=新的int[NumberOfIds];
//倒带文件
read.clear();
read.seekg(0,ios::beg);
int计数器=0;
NumberOfIds=0;
while(读>>n>>m){
if(tempNum!=n){
numberofidements[NumberOfIds]=计数器;
NumberOfIds++;
计数器=0;
}
tempNum=n;
计数器++;
i++;
}
对于(int k=0;k
现在我陷入了如何插入数据的困境。如果您有任何帮助,我们将不胜感激。您可以使用
std::vector
轻松做到这一点:
std::vector<std::vector<int>> arr;
while(read>>n>>m){
//if we're too small, push empty rows till we're big enough
while(n + 1 > arr.size()) {
arr.push_back(std::vector<int>());
}
arr.at(n).push_back(m); //push m onto row n
}
请参见此处的操作:这里是使用集合图的另一种方法:
#include <sstream>
#include <string>
#include <iostream>
#include <fstream>
#include <map>
#include <set>
int main()
{
std::map<int, std::set<int>> m;
std::ifstream ifs{"input.txt"};
// read
for(std::string line; std::getline(ifs, line);){
std::stringstream ss{line};
int key;
ss >> key;
for (int val; ss >> val;)
m[key].insert(val);
}
// print
for(auto i : m) {
std::cout << i.first << '\t';
for(auto j : i.second)
std::cout << j << ' ';
std::cout << '\n';
}
}
#包括
#包括
#包括
#包括
#包括
#包括
int main()
{
std::map m;
std::ifstream ifs{“input.txt”};
//阅读
for(std::string-line;std::getline(ifs,line);){
std::stringstream ss{line};
int键;
ss>>键;
用于(int-val;ss>>val;)
m[key].插入(val);
}
//印刷品
用于(自动i:m){
STD:谢谢你的回答。我是C++的新手。你能指导我如何打印向量的内容吗?你可以用一个数组来重复< <代码>:ST::向量< /代码>:<代码>(int x=0;x< AR.siz();x++){cout@scohe001 afaik基于迭代器的循环和新的基于范围的循环都不需要stl容器,它们都应该与c一起工作arrays@user463035818我想自从我开始使用基于范围的循环以来,我就不需要使用c风格的数组了……谢谢你的帮助!@thelaw vector实现的方法是通过查看我们当前已创建了多少行,然后查看下一个数字将属于哪一行。如果下一行大于我们已分配的行数,它将继续分配,直到我们有那么多行。如果您只有行1100,10000
的值,这可能是个问题。在这种情况下,您将最终创建10000
vectors和9998
将是无用的,而map实现将只生成3。
#include <sstream>
#include <string>
#include <iostream>
#include <fstream>
#include <map>
#include <set>
int main()
{
std::map<int, std::set<int>> m;
std::ifstream ifs{"input.txt"};
// read
for(std::string line; std::getline(ifs, line);){
std::stringstream ss{line};
int key;
ss >> key;
for (int val; ss >> val;)
m[key].insert(val);
}
// print
for(auto i : m) {
std::cout << i.first << '\t';
for(auto j : i.second)
std::cout << j << ' ';
std::cout << '\n';
}
}