Questions on std::string

This is a discussion on Questions on std::string within the C++ Programming forums, part of the General Programming Boards category; well the following worked for me: Code: #include <string> #include <vector> int main() { std::vector<std::string> vString(5); for (unsigned i=0; i<vString.size(); ...

  1. #16
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    well the following worked for me:
    Code:
    #include <string>
    #include <vector>
    
    int main()
    {
      std::vector<std::string> vString(5);
    
      for (unsigned i=0; i<vString.size(); i++)
      {
        vString[i] = "Test";
      }
    
      std::vector<std::string>::const_iterator it = vString.begin();
      while (it != vString.end() )
        cout<< *it++ << endl;
    }
    a iterator is basically a method for moving between the elements of a container. const_iterator is just one that doesn't allow you to change the elements' value

  2. #17
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I get a bunch of wierd errors
    Thantos' code wasn't meant to be cut and paste compiled. It omits parts and doesn't include the necessary headers.

    >what is the const_iterator in your example
    It's like a pointer that's specific to the vector.

    >do I need it to create a vector of strings?
    No, you can use integer indices if you want, or to be more correct, vector<string>::size_type indices.

    >What does it do?
    It gives you a way to portably point to the contents of a container object. You could use a string* and hope like hell that it works, but iterators are much better.
    My best code is written with the delete key.

  3. #18
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    I have one more question. I need to define and assign memory to a new std::string, I know how much I need to assign, but I don't want to initialize it to anything, how can I do that? What is the proper syntax?

  4. #19
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    well you'll have to initalize it to something. Why not null?
    Code:
    std::string str(10, '\0');

  5. #20
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    The const_iterator is a pointer, to go through all string stored int the buffer kept by vector. That cycle is simply a way to acess all strings. In Java you'll have something like and Iterator class and next() and hasNext() methods.
    I have two questions on std::strings, first of all, is there anyway to get the soucecode for string.cpp (or wherever the class is implemented)? Or, is there a way to get a complete list of member functions in std::string?
    The STL string is generaly a template class therefore the class's definition is in the header. Open <string> file in you compiler's include dir and check it out. I warn you that if you have STLport headers the code is pretty confusing and almost unreadble but it works very well.

    //edit
    post beated
    Code:
    //memory allocated globally and class constructed
    //free pointer later with delete
    std::string *my_string = new std::string("abc");
    
    //or, memory allocated locally, in function where it's declared
    //no need to use delete
    std::string my_string("abc");
    Last edited by xErath; 12-05-2004 at 12:48 AM.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 12-14-2007, 03:34 PM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 06:36 AM
  3. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  4. Debugging help
    By cuddlez.ini in forum C++ Programming
    Replies: 3
    Last Post: 10-24-2004, 08:08 PM
  5. DLL and std::string woes!
    By Magos in forum C++ Programming
    Replies: 7
    Last Post: 09-08-2004, 01:34 PM

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