Thread: help me find out why it is like this

  1. #16
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Link.

    Actually, I didn't know myself string had this method. I have always used += for appending.

    Code:
    std::string numericString;
    for(int i = 0; i < 5; i++){
        numericString += '1';
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  2. #17
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    Quote Originally Posted by laserlight View Post
    No, since it is an idiom for null terminated strings, which std::string is (generally) not. A simple index based for loop will suffice:
    Code:
    for (std::string::size_type i = 0, len = str.length(); i != len; ++i)
    {
        // Access str[i] here.
    }
    You could also use string iterators with a generic algorithm like say, std::copy_if (or at the moment: std::remove_copy_if since std::copy_if would only be around in C++0x).
    whats this ? and why is it there ? if we use using namespace std; do we still need to include "std::string::size_type" ?
    Last edited by Masterx; 01-23-2009 at 11:36 AM.
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Masterx
    whats this ? and why is it there ?
    What do you mean? I merely provided an example of how to use an index based for loop to iterate over the characters of a string. You would need to combine this with std::string's push_back(), or whatever method you wish to use to append to the destination string.

    Quote Originally Posted by Masterx
    if we use using namespace str; do we still need to include "std::string::size_type" ?
    You would not need to fully qualify std::string::size_type, so you could write it as string::size_type. std::string::size_type is the index type of std::string.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    Quote Originally Posted by laserlight View Post
    What do you mean? I merely provided an example of how to use an index based for loop to iterate over the characters of a string. You would need to combine this with std::string's push_back(), or whatever method you wish to use to append to the destination string.


    You would not need to fully qualify std::string::size_type, so you could write it as string::size_type. std::string::size_type is the index type of std::string.
    tanx alot. im going to test these . i'll let you know , if i was successful.
    Last edited by Masterx; 01-23-2009 at 11:49 AM.
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  5. #20
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    soorry to bother , thanks alot Dears. it went all fine , but i got some problem , i cant use "atoi() with string ! what should i do now ?atol() ? does it make it?
    Code:
    for (string::size_type i = found, len = str.length(); i != len; ++i)
        {
               currentcharacter = str[i];
               
               if ( (isspace(currentcharacter) ))
               {
                       
                       continue;
               }
               
              str1+= str[i];

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Masterx
    i cant use "atoi() with string ! what should i do now ?atol() ? does it make it?
    You actually "solved" this problem in your original code by using the c_str() member function of std::string.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    Quote Originally Posted by laserlight View Post
    You actually "solved" this problem in your original code by using the c_str() member function of std::string.
    yeah , tanx for saying that . but my intention of asking such a question was that , i just want to know if there is any equivalent for string in C++! ? is there ? !
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  8. #23
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    by the way , how could you guys understand that ptr was not allocated ! ? by experience or just its the knowledge of C++ that i can find in books/.?
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  9. #24
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Stringstreams.

    Code:
    string s("12345");
    int n;
    stringstream ss(s);
    ss >> n;
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  10. #25
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Masterx View Post
    by the way , how could you guys understand that ptr was not allocated ! ? by experience or just its the knowledge of C++ that i can find in books/.?
    Generally it's called "reading the code". You start at the top and you go to the bottom (going around and around in loops, naturally). You used ptr, but you never created any memory for it to point to.

  11. #26
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    Quote Originally Posted by anon View Post
    Stringstreams.

    Code:
    string s("12345");
    int n;
    stringstream ss(s);
    ss >> n;
    many tanx. now i got it .the use of stringstream that Elysia and laiserlight used to tell me multiple times and i couldnt understand how it works! lol)
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  12. #27
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    Quote Originally Posted by tabstop View Post
    Generally it's called "reading the code". You start at the top and you go to the bottom (going around and around in loops, naturally). You used ptr, but you never created any memory for it to point to.
    many tanx for the info dear tabstop.
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. could not find -lwsock32.lib
    By thomas_joyee in forum C++ Programming
    Replies: 8
    Last Post: 07-14-2008, 12:28 PM
  3. How to find O of threads ?
    By jabka in forum C Programming
    Replies: 3
    Last Post: 03-11-2008, 12:25 PM
  4. how do u find 2nd largest number??
    By juancardenas in forum C Programming
    Replies: 8
    Last Post: 02-14-2003, 08:28 AM
  5. Q: Recursion to find all paths of a maze
    By reti in forum C Programming
    Replies: 7
    Last Post: 11-26-2002, 09:28 AM