Thread: Question on std::string

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    124

    Question on std::string

    Hello all, hopefully a quick question for you guys.

    I'm trying to take a std::string object and be able to assign individual elements in it with arbitrary characters. Meaning, I want to be able to do something like:

    Code:
    string pString;
    for (int x = 0; x < 10; x++)
    {
           pString[x] = x;
    }
    How can I allocate memory to my std::string object without assigning specific values to it? Is it possible to do something like this?

    Code:
    string pString(10);
    Will that assign enough memory for 10 characters? (9 plus null terminator I mean).

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Will that assign enough memory for 10 characters? (9 plus null terminator I mean).
    Wow! How long would that take to test? 30 seconds? 1 full minute if you type slow?

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    http://www.cppreference.com/cppstring/

    One nice thing about std::string is that you don't need to worry about the null terminator.

    In your example you can use push_back()
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
            std::string str;
            for(char i='0'; i <= '9'; i++)
                    str.push_back( i );
            std::cout<<str<<std::endl;
    }

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    That's not really the answer I'm looking for. I want to be able to assign memory for use later, I don't want to have to use some function to fill a std::string with data already processed. I want to do the std::string equivalent of:
    Code:
    char* s = new char[10];
    But I don't want to have to create a pointer to a std::string.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I want to do the std::string equivalent of:

    char* s = new char[10];
    Why?

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    Gah, nevermind, I solved my problem using stringstream.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    10
    How can I allocate memory to my std::string object without assigning specific values to it? Is it possible to do something like this?

    Code:

    string pString(10);



    Will that assign enough memory for 10 characters? (9 plus null terminator I mean).
    That is exactly what you would do. Also, most implementations (i know SGI's does) of std::string reserve an extra byte when realloc'ing for the null terminator should the c_str() member function be called. For example, if you append a 9 character string to the end of a std::string with 9 reserved characters, the implementation would likely still call realloc() because it doesn't have the additional byte for the null terminator allocated.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    How can I allocate memory to my std::string object without assigning specific values to it? Is it possible to do something like this?

    string pString(10);

    Will that assign enough memory for 10 characters? (9 plus null terminator I mean).
    That is exactly what you would do.
    Really? When looking at the constructors for the std:string class, I didn't see any that took an int argument. In addition, my compiler gave me this error when I tested it:

    C:\Beginning C++\test2\main.cpp(14) : error C2664: '__thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >(const class std::allo
    cator<char> &)' : cannot convert parameter 1 from 'const int' to 'const class std::allocator<char> &'
    Reason: cannot convert from 'const int' to 'const class std::allocator<char>'
    No constructor could take the source type, or constructor overload resolution was ambiguous
    Last edited by 7stud; 11-25-2005 at 03:35 AM.

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    10
    Quote Originally Posted by 7stud
    Really? When looking at the constructors for the std:string class, I didn't see any that took an int argument. In addition, my compiler gave me this error when I tested it:

    C:\Beginning C++\test2\main.cpp(14) : error C2664: '__thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >(const class std::allo
    cator<char> &)' : cannot convert parameter 1 from 'const int' to 'const class std::allocator<char> &'
    Reason: cannot convert from 'const int' to 'const class std::allocator<char>'
    No constructor could take the source type, or constructor overload resolution was ambiguous
    Err, my bad, the constructor is actually
    Code:
    string( size_type length, const char& ch );
    http://www.cppreference.com/cppstrin...structors.html
    In this case the second argument could be any character.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging help
    By cuddlez.ini in forum C++ Programming
    Replies: 3
    Last Post: 10-24-2004, 07:08 PM
  3. DLL and std::string woes!
    By Magos in forum C++ Programming
    Replies: 7
    Last Post: 09-08-2004, 12:34 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. returning std::string
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 09-24-2001, 08:31 PM