Initializing an Array of Strings

I am trying to write a simpler code to initialize my string array instead of having to enumerate everything as I have done in my code. Is there a more efficient way to do this? Perhaps a for loop? But I don't know how I would use a for loop to initialize an array of strings... Any suggestions?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
	string space[196] = {"0","1","2","3","4","5","6","7","8","9",
						"10","11","12","13","14","15","16","17","18","19",
						"20","21","22","23","24","25","26","27","28","29",
						"30","31","32","33","34","35","36","37","38","39",
						"40","41","42","43","44","45","46","47","48","49",
						"50","51","52","53","54","55","56","57","58","59",
						"60","61","62","63","64","65","66","67","68","69",
						"70","71","72","73","74","75","76","77","78","79",
						"80","81","82","83","84","85","86","87","88","89",
						"90","91","92","93","94","95","96","97","98","99",
						"100","101","102","103","104","105","106","107","108","109",
						"110","111","112","113","114","115","116","117","118","119",
						"120","121","122","123","124","125","126","127","128","129",
						"130","131","132","133","134","135","136","137","138","139",
						"140","141","142","143","144","145","146","147","148","149",
						"150","151","152","153","154","155","156","157","158","159",
						"160","161","162","163","164","165","166","167","168","169",
						"170","171","172","173","174","175","176","177","178","179",
						"180","181","182","183","184","185","186","187","188","189",
						"190","191","192","193","194","195"};
Last edited on
One more question, the forum will not allow me to post another question... Hahaha. So I decided to post it here. I sincerely apologize for asking so many questions recently...

What is the difference between the following codes in their usage?

1
2
3
4
5
6
7
8
9
10
11
int main ()
{	
	int board_size = 5; 
	int **p_p_omok_board; 
	p_p_omok_board = new int*[board_size]; 
	for (int i = 0; i < board_size; i++)
	{
		p_p_omok_board[i] = new int[board_size]; 
	}
	return 0; 
}


1
2
3
4
5
6
int main ()
{	
	int board_size = 5; 
	int omok_board[board_size][board_size]; 
	return 0; 
}
1
2
3
4
5
const std::size_t N = 196 ;
std::string space[N] ;

// http://en.cppreference.com/w/cpp/string/basic_string/to_string
for( std::size_t i = 0 ; i < N ; ++i ) space[i] = std::to_string(i) ;
@JLBorges

Thank you so much for your response. I tried to imitate what you had written and when I try to compile my code, it states that "to_string was not declared in this scope". Any suggestions on how to fix this? I already added the <string> header as well.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
using namespace std; 

int main ()
{	
	int board_size; 
	cin >> board_size; 
	string **p_p_omok_board; 
	p_p_omok_board = new string*[board_size]; 

	for (int i = 0; i < board_size; i++)
	{
		p_p_omok_board[i] = new string[board_size]; 
	}

	int k = 0; 
	for (int i = 0; i < board_size; i++)
	{
		for (int j = 0; j < board_size; j++)
		{
			p_p_omok_board[i][j] = to_string(k); 
		}
	}
	return 0; 
}


Last edited on
> Any suggestions on how to fix this?

You appear to be using an old version of the compiler/library.
Consider updating to a more current version (which would have std::to_string())

In the interim, we can roll out our own (simplified) function to get the string representation of an int.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <string>
#include <sstream> // *** added
#include <vector> // *** added

namespace jhykima
{
    std::string int_to_string( int value ) // return the string representation of value
    {
        std::ostringstream stm ; // output string stream which writes to a string
        stm << value ; // write the integer
        return stm.str() ; // retrieve and return the string
    }
}

int main()
{
	std::size_t board_size; // http://en.cppreference.com/w/cpp/types/size_t
	std::cin >> board_size;

        // create a board_size X board_size board
        // favour using std::vector https://www.mochima.com/tutorials/vectors.html
	std::vector< std::vector<std::string> > omok_board( board_size, std::vector<std::string>(board_size) ) ;

	int seq_number = 0 ;

	for( std::size_t i = 0 ; i < board_size ; ++i )
            for( std::size_t j = 0 ; j < board_size ; ++j )
                omok_board[i][j] = jhykima::int_to_string(seq_number++) ;

	/*
	// C++11: favour a range-based loop to iterate over a range
	// http://www.stroustrup.com/C++11FAQ.html#for

	// C++11: auto: let the compiler figure out what the type of row is
	// http://www.stroustrup.com/C++11FAQ.html#auto

	for( auto& row : omok_board ) // for each row in omok_board
	{
            for( std::string& str : row ) // for each string in row
                str = jhykima::int_to_string(seq_number++) ;
	}
	*/
}
@JLBorges

Awesome. Thank you so much for your response. It worked! I think I am going to hold up on the vectors for now... My mentor has recommended that I should focus on the topics that I already know so far and internalize them. So I will hold off on learning about vectors. But seriously, thank you so much for putting up all these resources for me to look at!

I know you've already helped me so much but could you please answer my second question? Which is:

What is the difference between the following codes in their usage?
1
2
3
4
5
6
7
8
9
10
11
int main ()
{	
	int board_size = 5; 
	int **p_p_omok_board; 
	p_p_omok_board = new int*[board_size]; 
	for (int i = 0; i < board_size; i++)
	{
		p_p_omok_board[i] = new int[board_size]; 
	}
	return 0; 
}


1
2
3
4
5
6
int main ()
{	
	int board_size = 5; 
	int omok_board[board_size][board_size]; 
	return 0; 
}
Last edited on
Second one is non-conforming (t should be const int board_size ).

Other than that, in second snippet array is automatically allocated. You do not need to make any manipulations to make it work, it would be deallocated for you, generally access would be faster and it is true 2D array (in terms of memory layout). However stack space usually limited (so board_size = 1000 will likely to crash your program) and array dimensions should be known in compile time.
In first snippet you are allocating array of pointers to subarrays. You should make sure that it is deallocated correctly later, additional layer of indirections can make access slower and some tricks working on true 2D array will not work. On a plus side ypu can use whole memory and provide any dimension you want, be it hardcoded, user provided, etc...
@MiiNiPaa

Awesome. Thank you so much for the response!!! I understand better.

Best, Jae Kim
Topic archived. No new replies allowed.