Új hozzászólás Aktív témák
-
b.kov
senior tag
válasz
#74220800
#3893
üzenetére
Üdv!
Nem egy kezdőszelet, de ha tanulod a nyelvet, előbb-utóbb úgyis el fogod hagyni a tömböket valószínűleg, és helyette stl konténereket, és algoritmusokat fogsz használni.Itt van egy gyors szösszenet C++11-es módszerrel a problémádra:
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <iterator>
#include <algorithm>
// Returns a matrix (N x M), storing read data from file
std::vector<std::vector<int32_t>> readData(std::string fileName)
{
// Open file
std::ifstream input;
try
{
input.open(fileName);
}catch(std::ios_base::failure& e)
{
std::cerr << e.what() << std::endl;
return std::vector<std::vector<int32_t>>();
}
std::string line; // For reading line by line
input >> line; // Reading N
std::vector<std::vector<int32_t>> result(std::stoi(line)); // Returnable vector
std::getline(input, line); // Reading M, but do not store
// Write from file to matrix
for(auto& matrixLine : result)
{
std::getline(input, line);
std::istringstream stringOfNumbers(line); // Split line into individual strings
// Perform a transform for storing string chunks as integers in line of matrix
std::transform(std::istream_iterator<std::string>(stringOfNumbers),
std::istream_iterator<std::string>(),
std::back_inserter(matrixLine),
[](const std::string& stringOfNumber)
{
return std::stoi(stringOfNumber);
});
}
return result;
}
int main(int argc, char** argv)
{
std::vector<std::vector<int32_t>> result = readData("input.txt");
// Writing the output into stdout
for(auto line : result)
{
for(auto elem : line)
{
std::cout << elem << ", ";
}
std::cout << std::endl;
}
return 0;
}Természetesen nem teszteltem teljes körűen, csak egyetlen bemenetre, de arra működött.

input.txt:
5 7
2 3 4 -4 3 1 0
-3 2 1023 3 -32 8 9
-2 1 0 22 3 4 93
5 3 8 2 -9 3 -9321
2 3 4 -4 3 1 0
Új hozzászólás Aktív témák
● ha kódot szúrsz be, használd a PROGRAMKÓD formázási funkciót!
- BESZÁMÍTÁS! ASUS PRIME H510M i5 10400F 16GB DDR4 512GB SSD RX 6600 XT 8GB Chieftech BD-25B 600W
- Samsung Galaxy Ring 12mm, 1 Év Garanciával
- 14" Dell Latitude laptopok: 5400, 5480, 5490, 7480, E6410, E6440, E5450 / SZÁMLA + GARANCIA
- Telefon felvásárlás!! Apple Watch Series 6/Apple Watch Series 7/Apple Watch Series 8
- Telefon felvásárlás!! Samsung Galaxy S25, Samsung Galaxy S25 Plus, Samsung Galaxy S25 Ultra
Állásajánlatok
Cég: PCMENTOR SZERVIZ KFT.
Város: Budapest
Cég: NetGo.hu Kft.
Város: Gödöllő


