CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2005
    Posts
    9

    some basic questions on C++

    Hi gurues,
    this is a code which I took from somewhere I'm a newbie in C++,I cant get some of its part
    Code:
    void combs(const std::string &remaining_letters, const std::string &prefix, int letters_left_to_choose)
    {
      if (letters_left_to_choose <= 0) {
        std::cout << prefix << std::endl;
        return;
      }
      size_t i;
      std::string new_remaining;
      for (i = 0; i < remaining_letters.length(); ++i) {
        new_remaining = remaining_letters.substr(0, i) + remaining_letters.substr(i + 1);
        combs(new_remaining, prefix + remaining_letters[i], letters_left_to_choose - 1);
      }
    }
    I don't know what is the meaning of substr(i + 1)
    can someone explain?
    thanks.

  2. #2
    Join Date
    Jun 2004
    Posts
    106

    Re: some basic questions on C++

    I don't know anything about std::string, but that looks like "substring" to me - like CString's Mid function. So, substr(i+1) would mean to return the rest of the string starting at position i+1 (same as Right(i+1))
    Last edited by Yasoo; September 27th, 2005 at 08:12 PM.

  3. #3
    Join Date
    Jan 2001
    Posts
    588

    Re: some basic questions on C++

    Yasoo is correct. std::string::substr() returns a substring of the given string.

  4. #4
    Join Date
    Sep 2004
    Location
    A Planet Called Earth... :-)
    Posts
    835

    Re: some basic questions on C++

    I don't know what is the meaning of substr(i + 1)
    if u observe in ur code, u will find that it is a member of string class.

    Quote Originally Posted by MSDN
    basic_string substr(size_type pos = 0,
    size_type n = npos) const;
    The member function returns an object whose controlled sequence is a copy of up to n elements of the controlled sequence beginning at position pos.
    Substr will extract, n elements starting from pos. For eg:
    std::string str = "YOU ARE NOT WELCOME TO EARTH";
    std::string newStr = str.substr(12);
    Starting from 12'th position, extract remaining elements (position is zero based)
    So 12th position element is "W", and newStr will hold "WELCOME TO EARTH"
    std::string str = "YOU ARE NOT WELCOME TO EARTH";
    std::string newStr = str.substr(12, 7);
    Starting from 12'th position, extract the next 7 elements (position is zero based)
    So 12th position element is "W", and newStr will hold "WELCOME"
    C++ program ran... C++ program crashed... C++ programmer quit !!

    Regards

    Shaq

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured