std::string **array = new std::string*[width];
for( int i=0; i < width; ++i )
array[i] = new std::string[rows];
It's all sort of messy, especially considering you're going to have to loop through and deallocate the memory when you're done.
In all truth, you're best bet is to use std::vector. You can easily have vector of vector of strings too: std::vector<std::vector<std::string>> str_vecs;
EDIT: I had a spare couple of minutes at work. Here's a quick example.
#include <iostream>
#include <string>
#include <vector>
int main( int argc, char* argv[] )
{
std::vector<std::vector<std::string>> str_vec;
// You can get these values from std::cin
// I'm hard-coding for simplicity
int row = 5;
int width = 25;
// Vectors, by their nature, can resize dynamically
// However, we can also preset desired sizes
str_vec.resize( row );
for( auto &i : str_vec )
i.resize( width );
// You can assign and access just like a regular 2D array
str_vec[0][0] = "This is a string!";
std::cout << str_vec[0][0] << std::endl;
// You can get current size by using size()
std::cout << "Current size of row vector: " << str_vec.size() << std::endl;
std::cout << "Current size of width vector: " << str_vec[0].size() << std::endl;
return 0;
}
Crashing like "program stop working" "Process terminated with status -1073741510 (0 minutes, 12 seconds)", it crasching when i want put some information into strings. clines1(); scan file from which i read and put in numberlines number of rows.
true i think it will be problem somewhere there but idk where, also idk why it work with string array[25][width]; and dont work with allocating ? when it allocate it to same number :/
@iHutch105: Your examples are column-major. C/C++ is row-major. Nefri's attempt mixed the indices. On a square matrix one would not notice the error.
On the other hand there is reading from input too without checking whether (malformed) input adds too many words per line and too many lines.
std::ifstream opened with fstream::out flag?
The static array is a continuous block of memory. The dereference operation(s) on array does "pointer math" in order to retrieve desired element.
One can use dynamic 2D array with only 2 'new []' and 2 'delete []'. One reserves a block of rows*cols for the data, and the second reserves rows pointers to the beginning of each row within the block. On iHutch105's model there are multiple separate small blocks, rather than one for the data.
One could possibly use a std::string (*arr)[5]; instead of the array of pointers to data block(s), reducing the need to one 'new []' and one 'delete []'.
Cheers, Keskiverto. In hindsight, I should have probably gone with row/col, rather than row width, as I would have been less inclined to mix up the order that way. In fact, I'm not even sure why I didn't. I think I may have been blindly copying.
To JLBorges:
I trying use your code but, Codeblock same as Visual studio cant compile your file, i have around 20 errors there... Probebly it dont take 8 line using row_type = std::string[COLS] ;