954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Initializing an array of strings and printing it.

Hi,

Got another question concerning an array of strings:

I did it this way:

int main()
{	
	const std::string myArray[] = { "January", "February", "March", "April", "May", "June", "July",
		"August", "September", "October", "November", "December"};

	for (size_t i = 0; i < 12; i++)
		std::cout << myArray[i] << '\n';
	std::cout << '\n';

	std::cin.get();

	return 0;
}


but I thought that there was also a way that you could use the beginning and end of the array to loop threw the array like this myArray.begin() and myArray.end() doing something like this:

std::string::iterator itr;

	for (itr = myArray.begin(); itr != myArray.end(); itr++)
		std::cout << *itr << '\n';
	std::cout << '\n';


Problem I have is that I'm using an array of strings, I therefore used the following myArray[0].begin() and myArray[11].end() but that doesn't seem to work.

Could anyone help me out, thank you.

JoBe
Posting Pro in Training
420 posts since Sep 2004
Reputation Points: 51
Solved Threads: 4
 

Well, I found a solution, but I'm not sure this is good, to find the starting point was simple, but, I find to use the end of the array of strings not quiet right. Here's how I did it:

for (std::string *str = myString; str != &myString[12]; str++)
	         std::cout << *str << '\n';
	std::cout << '\n';
JoBe
Posting Pro in Training
420 posts since Sep 2004
Reputation Points: 51
Solved Threads: 4
 
but I thought that there was also a way that you could use the beginning and end of the array to loop threw the array like this .

use vector and you can do what you want using iterators.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

How about something like:

for( const std::string* str = myArray ; str != 0 ; str++ )
        std::cout << *str << '\n';
    std::cout << '\n';
~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

@ AD,

Thanks AD, but, I thought that begin and end also worked with strings no?


@ s.o.s,
That doesn't work s.o.s, I think it's because strings are not \0 terminated.

Got an additional question gentlemen, there's an exercise that asks the following:

- Find an example where it would make sense to use a name in its own initializer.

I quiete frankly don't understand what is asked here? Do you guys know?

JoBe
Posting Pro in Training
420 posts since Sep 2004
Reputation Points: 51
Solved Threads: 4
 
@ AD, Thanks AD, but, I thought that begin and end also worked with strings no?


Iterators work only for C++ containers. String, Vector etc. are containers and iterators work well with them. Array is not a C++ container class. You can use iterator to walk through the characters of the string..@ s.o.s,
That doesn't work s.o.s, I think it's because strings are not \0 terminated.
If you will look closely, I have done "str != 0", here str is a pointer. The above code works perfectly well for me.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

Well s.o.s, using this code:

#include <iostream>
#include <string>

int main()
{	
	std::string myArray[] = { "January", "February", "March", "April", "May", "June", "July",
		"August", "September", "October", "November", "December"};

	for(const std::string* str = myArray ; str != 0; str++ )
                     std::cout << *str << '\n';
                std::cout << '\n';
				
	std::cin.get();

	return 0;
}

It compiles without errors or warnings, but once executed, it goes on and on, doesn't seem to find the end of the array.

JoBe
Posting Pro in Training
420 posts since Sep 2004
Reputation Points: 51
Solved Threads: 4
 

Okay looks like something queer is going on around here. I ran the same program without any modifications and this is what I get:

January
February
March
April
May
June
July
August
September
October
November
December

Press ENTER to continue.
~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

Well, Ive checked and checked again, the code still gives the same output, it compiles correctly, no errors nor warnings but once executed, it just keeps on going. If I use str != &myString[12] however, it works like a charm.

Guess it's a mistery that won't be resolved. Anyway, thanks for your help guys :!:

JoBe
Posting Pro in Training
420 posts since Sep 2004
Reputation Points: 51
Solved Threads: 4
 

>>for(const std::string* str = myArray ; str != 0; str++ )

Now how can that ever work because myArray is not a nulll-terminated array. You are attempting to apply C-style character array techniques to a c++ string, and won't work.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

I know it was wrong and was going to edit the post but dont know why it works in my case....

Just wanted to make someone double cheked it because it works in my case( dont know how ).

Mr. Dragon, if you have a VS2005, can you please check it out because Jobe has I think got a VC6.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

Mr SOS -- it works up to a point -- gives the famous message box that a fatal error has occured -- similar to core dump in *nix.

Jo: There is a very simple fix to the problem -- check for an empty string

int main()
{	
	std::string myArray[] = { "January", "February", "March", "April", "May", "June", "July",
		"August", "September", "October", "November", "December", ""};

	for(const std::string* str = myArray ; *str != ""; str++ )
                     std::cout << *str << '\n';
                std::cout << '\n';
				
	std::cin.get();

	return 0;
}
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
...because Jobe has I think got a VC6.

Hi gentlemen,

@s.o.s,
Ive got VS EE free version.

@ Ancient Dragon,
Thanks AD, I thought there ought to be a cleaner solution then (std::string *str = myString; str != &myString[12]; str++)

Also tried to use the vector AD:

#include <iostream>
#include <string>
#include <vector>

void print(const std::vector<std::string>);

int main()
{	
	std::vector<std::string> myString;
	
	myString.push_back("January");
	myString.push_back("February");
	myString.push_back("March");
	myString.push_back("April");
	myString.push_back("May");
	myString.push_back("June");
	myString.push_back("July");
	myString.push_back("August");
	myString.push_back("September");
	myString.push_back("October");
	myString.push_back("November");
	myString.push_back("December");

	print(myString);

	std::cin.get();

	return 0;
}

void print(const std::vector<std::string> myArray)
{
	std::vector<std::string>::const_iterator itr;

	for (itr = myArray.begin(); itr != myArray.end(); itr++)
		std::cout << *itr << '\n';
}


Anyway, thank you both gentlemen for the great help :!:

JoBe
Posting Pro in Training
420 posts since Sep 2004
Reputation Points: 51
Solved Threads: 4
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You