Creating a two-dimensional vector from a file? - c++

I'm trying to read data from a grid, in a 20x20 file. I'm using a two-dimensional vector of vectors of strings.
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
#define HEIGHT 20
#define WIDTH 20
typedef vector<vector<string> > stringGrid;
bool readGrid(stringGrid& grid, string filename) {
grid.resize(HEIGHT);
for (int i = 0; i < HEIGHT; i++)
grid[i].resize(WIDTH);
ifstream file;
string line;
file.open(filename.c_str());
if (!file.is_open()) {
return false;
}
for (int i = 0; i < HEIGHT; i++)
{
while (getline(file, line)) {
grid[i].push_back(line);
}
}
return true;
}
void displayGrid(stringGrid grid)
{
for (int row = 0; row < HEIGHT; row++)
{
for (int col = 0; col < WIDTH; col++)
{
cout << grid[col][row];
}
cout << endl;
}
}
int main(){
stringGrid grid;
readGrid(grid, "test.txt");
displayGrid(grid);
return 0;
}
However, when I run this code, the program only outputs a few blank lines.
Why doesn't this code work? The logic seems sound enough.

You can do what Kocik said or you could use vector::reserve instead of vector::resize in your code. It should then work.
reserve simply reserves enough memory to avoid reallocating memory while it's pushing back items.
resize actually resizes the vector by adding n default items. In your case, those items are empty vectors of strings, so you'll get 20 of them in your vector before any other items you subsequently push back.
Here is a bit more info about the difference between the two methods.

You use grid[i].push_back(line); after you created 20 elements with grid[i].resize(WIDTH);. Method push_back add new element on the end of the vector, so new elements will have index 21, 22 .. 40.
You have two options:
you can set new values to initialized elements with some iterator, or
you can just skip resize and push new elements to empty vector. Simply delete those two lines:
for (int i = 0; i < HEIGHT; i++)
grid[i].resize(WIDTH);</li>

As an aside, it's generally considered bad practice to use a vector-of-vectors. Instead, use a class that wraps a single vector and does index arithmetic (either x*h+y or y*w+x depending on which major order you want).

Related

Setting-up a 2D array using std::Vector

I have already improved my array from this question in order to be created within a class, I also kind of optimized it using std::vector so it is not allocated on a heap but on a stack and thus it is faster and also I do not have to manage the memory manually.
But I was not able to create a function which would set a value to an element. I got errors like expression must have pointer-to-object type (when I try to declare my element as int cell or an array may not have element of this type (when I try to declare it as int cell[][]) and I get a segmentation error when I try to declare it like this int *cell[].
Here is my code
.hpp
#pragma once
#include <iostream>
#include <vector>
class myPlan
{
int slots;
int rows = 3;
int cell[][];
public:
myPlan(int slots);
void setCellValue(int row, int slot, int value)
};
.cpp
#include "myPlan.hpp"
myPlan::myPlan(int slots)
{
this->slots = slots;
std::vector<std::vector<int> > cell(STATUS_N);
for (int i = 0; i < STATUS_N; i++)
{
// declare the i-th row to size of column
cell[i] = std::vector<int>(slots);
}
//Print the array
for (int i = 0; i < STATUS_N; i++) {
for (int j = 0; j < cell[i].size(); j++)
{
cell[i][j] = 0;
std::cout << cell[i][j] << " ";
}
std::cout << "\n";
}
}
void myPlan::setCellValue(int row, int slot, int value)
{
cell[row][slot] = value;
}
main.cpp
#include "myPlan.hpp"
int main()
{
myPlan plan(N_PLAN);
plan.setCellValue(0,2,42);
}
Thank you very much in advance for any help
P.S. I hope that this is more optimal than this array, if there was even better optimized version, I would be glad to learn about it.
So here's what I think is the fundamental problem, it has nothing to do with arrays or vectors. You are just handling your class variable incorrectly. Here's your class
class myPlan
{
int slots;
int rows = 3;
int cell[][];
int cell[][]; is not legal C++, but we'll let that pass. The point is that you have some kind of 2D array called cell in your class.
Now here's your constructor
myPlan::myPlan(int slots)
{
this->slots = slots;
std::vector<std::vector<int> > cell(STATUS_N);
for (int i = 0; i < STATUS_N; i++)
{
// declare the i-th row to size of column
cell[i] = std::vector<int>(slots);
}
Now here you've declared a 2D vector called cell. But (and here's the point) this is not the same cell that's in your class, it a completely separate variable which just happens to have the same name. And like any variable declared inside a function it will no longer exist after the function exits.
Here's how it should be done.
class myPlan
{
int slots;
int rows = 3;
std::vector<std::vector<int> > cell;
...
myPlan::myPlan(int slots)
{
this->slots = slots;
cell.resize(STATUS_N);
for (int i = 0; i < STATUS_N; i++)
{
// declare the i-th row to size of column
cell[i] = std::vector<int>(slots);
}
See the difference? I didn't declare a new cell variable, I just resized the one that is declared in the class.
As far as I understood your problem, you need an array of arrays (a number of slots), which size is known at compile time. And you prefer it to be on the stack.
I encourage you to use std::array and forget about C-style arrays ([] - these guys). Or better learn the difference between them and make your own mind.
There is an example how to use it. The class has to be templated to provide parameters of the array sizes at compile time. It is pretty straighforward, but if you don't need it, you can just remove the template line before the class definition and replace RowNumber and SlotSize with you defines or constants known at compile time.
This solution will only work if sizes are known at compile time. If you want to provide the number of rows as myPlan constructor parameter, then you'll need to dynamically allocate memory and use std::vector or something similar.
#include <iostream>
#include <array>
template<std::size_t RowNumber, std::size_t SlotSize>
class myPlan {
std::array<std::array<int, SlotSize>, RowNumber> cells;
public:
myPlan() :
cells{} // initialize array with zeros
{
//Print the array
for (int i = 0; i < cells.size(); i++) {
for (int j = 0; j < cells[i].size(); j++)
{
std::cout << cells[i][j] << " ";
}
std::cout << "\n";
}
}
void setCellValue(int row, int slot, int value) {
cells[row][slot] = value;
}
};
#define N_PLAN 4
#define STATUS_N 3
int main()
{
myPlan<N_PLAN, STATUS_N> plan;
plan.setCellValue(0,2,42);
}

Dynamically allocating memory

I am new to C++ and programming in general so i apologize if this is a trivial question.I am trying to initialize 2 arrays of size [600][600] and type str but my program keeps crashing.I think this is because these 2 arrays exceed the memory limits of the stack.Also,N is given by user so i am not quite sure if i can use new here because it is not a constant expression.
My code:
#include<iostream>
using namespace std;
struct str {
int x;
int y;
int z;
};
int main(){
cin>>N;
str Array1[N][N]; //N can be up to 200
str Array2[N][N];
};
How could i initialize them in heap?I know that for a 1-D array i can use a vector but i don't know if this can somehow be applied to a 2-D array.
How 2-or-more-dimensional arrays work in C++
A 1D array is simple to implement and dereference. Assuming the array name is arr, it only requires one dereference to get access to an element.
Arrays with 2 or more dimensions, whether dynamic or stack-based, require more steps to create and access. To draw an analogy between a matrix and this, if arr is a 2D array and you want access to a specific element, let's say arr[row][col], there are actually 2 dereferences in this step. The first one, arr[row], gives you access to the row-th row of col elements. The second and final one, arr[row][col] reaches the exact element that you need.
Because arr[row][col] requires 2 dereferences for one to gain access, arr is no longer a pointer, but a pointer to pointer. With regards to the above, the first dereference gives you a pointer to a specific row (a 1D array), while the second dereference gives the actual element.
Thus, dynamic 2D arrays require you to have a pointer to pointer.
To allocate a dynamic 2D array with size given at runtime
First, you need to create an array of pointers to pointers to your data type of choice. Since yours is string, one way of doing it is:
std::cin >> N;
std::string **matrix = new string*[N];
You have allocated an array of row pointers. The final step is to loop through all the elements and allocate the columns themselves:
for (int index = 0; index < N; ++index) {
matrix[index] = new string[N];
}
Now you can dereference it just like you would a normal 2D grid:
// assuming you have stored data in the grid
for (int row = 0; row < N; ++row) {
for (int col = 0; col < N; ++col) {
std::cout << matrix[row][col] << std::endl;
}
}
One thing to note: dynamic arrays are more computationally-expensive than their regular, stack-based counterparts. If possible, opt to use STL containers instead, like std::vector.
Edit: To free the matrix, you go "backwards":
// free all the columns
for (int col = 0; col < N; ++col) {
delete [] matrix[col];
}
// free the list of rows
delete [] matrix;
When wanting to allocate a 2D array in C++ using the new operator, you must declare a (*pointer-to-array)[N] and then allocate with new type [N][N];
For example, you can declare and allocate for your Array1 as follows:
#define N 200
struct str {
int x, y, z;
};
int main (void) {
str (*Array1)[N] = new str[N][N]; /* allocate */
/* use Array1 as 2D array */
delete [] Array1; /* free memory */
}
However, ideally, you would want to let the C++ containers library type vector handle the memory management for your. For instance you can:
#include<vector>
..
std::vector <std::vector <str>> Array1;
Then to fill Array1, fill a temporary std::vector<str> tmp; for each row (1D array) of str and then Array1.push_back(tmp); to add the filled tmp vector to your Array1. Your access can still be 2D indexing (e.g. Array1[a][b].x, Array1[a][b].y, ..., but you benefit from auto-memory management provided by the container. Much more robust and less error prone than handling the memory yourself.
Normally, you can initialize memory in heap by using 'new' operator.
Hope this can help you:
// Example program
#include <iostream>
struct str {
int x;
int y;
int z;
};
int main()
{
int N;
std::cin>>N;
str **Array1 = new str*[N]; //N can be up to 200
for (int i = 0; i < N; ++i) {
Array1[i] = new str[N];
}
// set value
for (int row = 0; row < N; ++row) {
for (int col = 0; col < N; ++col) {
Array1[row][col].x=10;
Array1[row][col].y=10;
Array1[row][col].z=10;
}
}
// get value
for (int row = 0; row < N; ++row) {
for (int col = 0; col < N; ++col) {
std::cout << Array1[row][col].x << std::endl;
std::cout << Array1[row][col].y << std::endl;
std::cout << Array1[row][col].z << std::endl;
}
}
}

I get the error invalid types 'float[int]' for array subscript?

I am quite new to programming,so I really need help. I need to wrtie a function which produce 2d arrays with random values. here is my code:
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
float randArray(int row, int column);
int main()
{
int r = 10, c = 8;
float fckMmd = randArray(r,c);
///printing the array:
for (int row=0; row<r; row++){
for (int column=0; column<c; column++){
cout << fckMmd[row][column] << " ";
}
cout << endl;
}
}
float randArray(int row, int column){
srand(time(NULL));
float *randArr;
randArr = new int [row][column];
for(int k=0; k<row; k++){
for(int kk=0; kk<column; kk++){
randArr[k][kk] = rand();
}
}
return randArr;
}
But I get the error mentioned above. Where is the problem? help me please
randArr is a float * but you try to allocate a 2d array in it. A 2d array is not the same thing as a pointer. Your function only returns 1 float as well. I suggest you use vectors (also so you don't leak memory). Furthermore you should only call srand ONCE, not every time, and be aware rand() returns an integer, not a floating point value.
std::vector<std::vector<float>> randArray(int row, int column)
{
std::vector<std::vector<float>> randArr(row);
for (auto& v : randArr)
{
v.resize(column);
}
for(int k=0; k<row; k++)
{
for(int kk=0; kk<column; kk++)
{
randArr[k][kk] = static_cast<float>(rand());
}
}
return randArr;
}
It's because fckMmd is only a float and not a pointer or array.
First:
float *randArr;
declares a pointer to float. You then do
randArr = new int [row][column];
which allocates memory for a 2D array of ints (incompatible types, technically you allocate memory for a pointer to arrays of type int[column]), hence the error.
You're better using a std::vector instead, or, if you want a manually-managed dynamically allocated 2D array, use float **randArr; instead, and allocate
float** randArr;
randArr = new float* [rows];
for(int i = 0; i < row; ++i)
randArr[i] = new float[col];
or
float (*randArr)[col]; // pointer to array float[col]
randArr = new float[row][col];
Other issues: most of the time, srand must be used only once in the program. It is a good idea to call it in main() and not bury it into a function, since you may end up calling the function multiple times.
Last issue: if you want speed, you're better off using a single flat array (or std::vector) and map from 2D to 1D and vice versa, since your data will be guaranteed to be contiguous and you'll have very few cache misses.

C++ and Root, passing 2D array to function and use it in TGraph

How can I pass a 2D array into a function and use it like myArray[i][j] but without knowing the size of that array inside that function?
I can know the size inside the main.
I want to use it like this:
TGraph *myGraph = new TGraph(nValues, myArray[0][j], myArray[1][j]);
// I'll not use a loop for j, since TGraph receives all the values in the array,
like "x values" and "y values"
If I do it like this it works, but I would have to pass to the function Col1 and Col2 that are two 1D arrays:
main() {
...
graphWaveTransmittance("a", nValues, Col1, Col2,
" Au ", "Evaporated", "thickness 5nm", kGreen+1);
...
}
void graphWaveTransmittance(char *n, int nValues, float Param1[], float Param2[],
char *title, char *header, char *entry, Color_t color) {
TGraph *myGraph = new TGraph(nValues, Param1, Param2);
...
}
The Array:
float valuesArray[nCol][nValues];
for(int y=0; y<nValues; y++){
for (int i=0; i<nCol; i++) {
valuesArray[i][y] = values[i][y];
}
i=0;
}
Note: I've made it like this because values[ ][ ] is an array with values that are read from a text file. Before read the file I don't know how many lines are going to be necessary. With this second array (valuesArray[ ][ ]) I can make it to have just the size of the number of values that are read.
Firstly, I've put all the values in values[ ][ ] with "-1" and it's size very large. Then I've counted the number of lines and just used that value for valuesArray[ ][ ]. This is the first array with values (the large one):
const int nCol = countCols;
float values[nCol][nLin];
// reads file to end of *file*, not line
while(!inFile.eof()) {
for(int y=0; y<nLin; y++){
for (int i=0; i<nCol; i++) {
inFile >> values[i][y];
}
i=0;
}
}
One other question, I've seen that "while(!inFile.eof())" shouldn't be used. What can I use instead? (I don't know the total number of lines from the .txt file at this point)
Importing values in columns in a .txt, till now I have:
vector<vector<float> > vecValues; // your entire data-set of values
vector<float> line(nCol, -1.0); // create one line of nCol size and fill with -1
bool done = false;
while (!done)
{
for (int i = 0; !done && i < nCol; i++)
{
done = !(inFile2 >> line[i]);
}
vecValues.push_back(line);
}
The problem of this is that the values are like vecValues[value][column number from .txt]
I want to have vecValues[column number from .txt][value].
How can I change it?
I'm reading from the file like this:
main() {
...
vector < vector <float> > vecValues; // 2d array as a vector of vectors
vector <float> rowVector(nCol); // vector to add into 'array' (represents a row)
int row = 0; // Row counter
// Dynamically store data into array
while (!inFile2.eof()) { // ... and while there are no errors,
vecValues.push_back(rowVector); // add a new row,
for (int col=0; col<nCol; col++) {
inFile2 >> vecValues[row][col]; // fill the row with col elements
}
row++; // Keep track of actual row
}
graphWaveTransmittance("a", nValues, vecValues, " Au ",
"Evaporated", "thickness 5nm", kGreen+1);
// nValues is the number of lines of .txt file
...
}
//****** Function *******//
void graphWaveTransmittance(char *n, int nValues,
const vector<vector <float> > & Param, char *title, char *header,
char *entry, Color_t color) {
// like this the graph is not good
TGraph *gr_WTransm = new TGraph(nValues, &Param[0][0], &Param[1][0]);
// or like this
TGraph *gr_WTransm = new TGraph(Param[0].size(), &Param[0][0], &Param[1][0]);
Note: TGraph can accept floats, my previous arrays were floats
Do you know why the graph is not appearing correctly?
Thank you
I ran into a similar problem recently. I ended up using two dimensional vectors, which don't need to know the inner dimension when passed to a function.
Declare vectors like this
vector< vector<int> > vec(xRange, vector<int>(yRange, initialValue));
While replacing xRange with your size in the x dimension, yRange with your size in the y direction, and initialValue with what you want to initialize your 2d vector with.
At this point, you can access or update vector contents using
vec[x][y]
To pass this to a function, use this
void myFunc(std::vector< std::vector<int> >& vec) {
Be sure to
#include <vector>
You can use vectors to sove this problem as Michael Parker already mentioned. To make it work in root with graphs, note that TGraph expects two arrays of double or float as parameters.
Therefore you have to use
std::vector<std::vector<double> >
or
std::vector<std::vector<float> >
in your case.
Then your function looks like:
void drawGraph(const std::vector<std::vector<double> > & data)
{
TGraph* graph = new TGraph(data[0].size(), &data[0][0], &data[1][0]);
//...
}
The &data[0][0] "converts" the vectors to arrays as needed by TGraph.
Concerning your second question, instead of using !inFile.eof(), I typically directly ask whether the reading process was succesful, i.e. in your case:
if(!(inFile >> values[i][y]))
{
//at end of file
}
I prefer to use this in a while loop but thats a matter of taste.
By the way, by using vectors you no longer need to run though the whole file in advance to count the number of lines, just use push_back(...).
If you do not know the number of lines nor the number of columns, you can use getline to read out the file and to determine the number of columns:
std::vector<std::vector<float> > data;
std::string buffer;
getline(infile,buffer)//I do not check that the file has at least one line here
istringstream firstLine(buffer);
float value;
while(firstline >> value)
{
data.push_back(std::vector<float>(1,value));
}
while(getline(infile,buffer))
{
istringstream line(buffer);
float value;
unsigned int counter = 0;
while(line >> value)
{
data[counter].push_back(value));
counter++;
}
}
Note that this requires:
#include<sstream>
and that this assumes that the number of columns does not change over the file size.

Push_back 1D Vector as Row into 2D Vector Array

I'm trying to define the values of a 'row' or 1D vector and then push_back that row into a 2D vector. I've tried a couple different things that don't throw errors but also don't seem to work. Code below:
#include <vector>
#include <iostream>
using std::vector;
#define HEIGHT 5
#define WIDTH 3
// 2D VECTOR ARRAY EXAMPLE
int main() {
vector<vector<double> > array2D;
vector<double> currentRow;
// Set up sizes. (HEIGHT x WIDTH)
// 2D resize
array2D.resize(HEIGHT);
for (int i = 0; i < HEIGHT; ++i)
{
array2D[i].resize(WIDTH);
}
// Try putting some values in
array2D[1][2] = 6.0; // this works
array2D[2].push_back(45); // this value doesn't appear in vector output. Why?
// 1D resize
currentRow.resize(3);
// Insert values into row
currentRow[0] = 1;
currentRow[1] = 12.76;
currentRow[2] = 3;
// Push row into 2D array
array2D.push_back(currentRow); // this row doesn't appear in value output. Why?
// Output all values
for (int i = 0; i < HEIGHT; ++i)
{
for (int j = 0; j < WIDTH; ++j)
{
std::cout << array2D[i][j] << '\t';
}
std::cout << std::endl;
}
return 0;
}
By the time you push_back currentRow, array2D already contains HEIGHT rows and after the push_back it will contain HEIGHT+1 rows. You just don't show the last one you added, only the first HEIGHT rows.
push_back() appends an item to the end of the vector. If the vector contained WIDTH items, it contains WIDTH+1 items after the push_back().
When you print the contents of the vector, you only print the first WIDTH items, even if the vector contains more, so you don't see the additional items.
You can find out how many items are in a vector with the size() method.
You are using resize when you actually want to use reserve method instead. The thing is that resize does change the content of the vector, while reserve just changes the storage capacity. So if you resize a vector to N elements and then push some elements into it, they will be pushed in positions N, N + 1, etc, while if you just reserve size for N elements, they will be pushed in positions 0, 1, etc, which is what you seem to want.