如何在C ++中的二维数组中插入可变数量的数据
How to insert variable amount of data in 2d array in c++
我有以下文件 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>>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]];
现在我卡在了如何插入数据上。任何帮助将不胜感激。
你可以很容易地用 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
}
或者,如果您的第一列数字变大(因此您不想创建很多空行),您可以使用 std::map
:
std::map<int, std::vector<int>> arr;
while(read>>n>>m){
//works because map subscript creates a key/val pair if one doesn't exist
//so if this row doesn't exist, we get a new vector to push_back to
arr[n].push_back(m); //push m onto row n
}
在此处查看实际效果:ideone
这是另一种使用集合映射的方法:
#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';
}
}
根据您对排序或保留重复项的要求,您可能 select 其他容器。
我有以下文件 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>>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]];
现在我卡在了如何插入数据上。任何帮助将不胜感激。
你可以很容易地用 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
}
或者,如果您的第一列数字变大(因此您不想创建很多空行),您可以使用 std::map
:
std::map<int, std::vector<int>> arr;
while(read>>n>>m){
//works because map subscript creates a key/val pair if one doesn't exist
//so if this row doesn't exist, we get a new vector to push_back to
arr[n].push_back(m); //push m onto row n
}
在此处查看实际效果:ideone
这是另一种使用集合映射的方法:
#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';
}
}
根据您对排序或保留重复项的要求,您可能 select 其他容器。