beginner: dynamic array of strings

This is a discussion on beginner: dynamic array of strings within the C++ Programming forums, part of the General Programming Boards category; salutations, we are beginners. we are trying to create a dynamic array of strings. we tried it in C, but, ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    49

    beginner: dynamic array of strings

    salutations,

    we are beginners.
    we are trying to create a dynamic array of strings. we tried it in C, but, searching in the internet, we perceived that most people agreed that using std::vector in C++ is much better than relying on malloc and realloc in C to do a dynamic char**.
    so, we are beginners in C++.
    basically, what we want to do is to be able to, inside a for, add as many strings as we want into an array, without previously knowing how many elements we will have to add in each iteration of the for, and what the number of elements of the array will be.
    note: this is not a homework problem.
    thanks in advance.
    Last edited by pc2-brazil; 04-28-2008 at 04:09 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    Create your vector before the for loop. In the loop, add each string to the vector using the push_back function. When the loop is done, the vector will hold however many strings you added.

    If you're having problems implementing that, post your code and the problems you're having.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by pc2-brazil View Post
    so, we are beginners in C++.
    Start here:

    http://www.mindview.net/Books/TICPP/...ngInCPP2e.html

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    49
    Quote Originally Posted by Daved View Post
    Create your vector before the for loop. In the loop, add each string to the vector using the push_back function. When the loop is done, the vector will hold however many strings you added.

    If you're having problems implementing that, post your code and the problems you're having.
    we were in doubt about whether we should post this code in the first post, but here it is:
    Code:
    #include<vector>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    	vector<string> array;
    	array.push_back("item1");
    	array.push_back("item2");
    
    	for (int a=0; a<array.size(); a++){
    		printf("%s\n",array[a]);
    	}
    	return 0;
    }
    the idea is that the items are added, and then accessed in a for from 0 to the total number of items - 1. array.push_back seems to work, but array[a] crashes.
    this seems very simple; it is based on an on-line tutorial. we should be able to make this work.
    thank you in advance.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    you need to use the c_str() member to get a C-string instead of a class std::string.
    Code:
    printf("%s\n",array[a].c_str());
    Alternatively, you could use std::cout instead:
    Code:
    std::cout << array[a] << '\n';
    You would have to #include <iosteam> for std::cout.
    Last edited by robwhit; 04-28-2008 at 07:21 PM.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You also forgot to #include <string>
    and from the code you posted, there is no need to #include <cstring> since you aren't using any old C string functions.

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Here is your example cleaned up a bit, showing the use of iterators.

    Code:
    #include <iostream> 
    #include <vector>
    #include <string>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    	vector<string> array;
    	vector<string>::iterator iter ; 
    	array.push_back("item1");
    	array.push_back("item2");
    
    	for (iter = array.begin() ; iter != array.end() ; iter++){
    		cout << *iter << endl ; 
    	}
    	return 0;
    }
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    49
    thank you for the responses, it was very helpful.
    the iterator example worked.
    but when we try to use printf, including c_str, like this:
    Code:
    #include<string>
    #include<vector>
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    	vector<string> array;
    	array.push_back("item1");
    	array.push_back("item2");
    
    	for (int a=0; a<array.size(); a++){
            printf("%s\n",array[a].c_str);
    	}
    	return 0;
    }
    it doesn't crash, but it gives a strange output:
    Code:
    кн♣
    кн♣
    Press any key to continue
    thank you in advance.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    You're missing the parentheses after c_str. It's a function.

    Note that this is a good reason to use cout instead of printf- the compiler would have caught that error.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    You forgot to use the parentheses.

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    49
    of course, the parentheses. this was a beginner mistake.
    thank you for the responses.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of strings?
    By mc72 in forum C Programming
    Replies: 5
    Last Post: 11-15-2008, 11:15 PM
  2. Multi-dimensional dynamic array - performance
    By melkor445 in forum C++ Programming
    Replies: 15
    Last Post: 10-07-2007, 04:17 AM
  3. Dynamic structure with array and array count
    By Nazgulled in forum C Programming
    Replies: 14
    Last Post: 06-08-2007, 10:10 PM
  4. Array of strings in C
    By szill in forum C Programming
    Replies: 10
    Last Post: 02-22-2005, 04:03 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21