|
-
September 27th, 2005, 06:28 PM
#1
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.
-
September 27th, 2005, 08:10 PM
#2
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.
-
September 27th, 2005, 09:00 PM
#3
Re: some basic questions on C++
Yasoo is correct. std::string::substr() returns a substring of the given string.
-
September 27th, 2005, 09:17 PM
#4
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.
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|