1

I am C++ beginner. I have a code as below

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;
int main()
{
   char sym[] = "Audi.Despo";
   //string sym ("Audi.Despo");
    string rs(sym);
    //cout << rs << endl;
    rs = string(sym,4);

 cout << rs;

    return 0;
}

If the variable sym is a char array the final output of the string variable is Audi

but if the same variable sym is a string the final output is .Despo (the suffix is printed)

Whats the explanation.

Looks like the line

rs = string(sym,4);

changes its behaviour if its input is a char [] or a string.

1

3 Answers 3

4

That's just the different implementations of two of the constructor overloads:

string ( const string& str, size_t pos, size_t n = npos );

Content is initialized to a copy of a substring of str. The substring is the portion of str that begins at the character position pos and takes up to n characters (it takes less than n if the end of str is reached before).

string ( const char * s, size_t n );

Content is initialized to a copy of the string formed by the first n characters in the array of characters pointed by s.

Sign up to request clarification or add additional context in comments.

Comments

0
string ( const string& str, size_t pos, size_t n = npos );

Second argument in std::string's constructor is starting position of substring that is used to initialize content.

Comments

0

When you say:

rs=string(sym, 4);

What you are doing is saying, "build me a std::string object, and I (the programmer) will help you (the program) do that by giving you this character array and a number." The code behind std::string class knows that in that instance, where it is given a character array and an integer, that it should take the number of characters indicated from the character array and construct a std::string using that content.

When you say:

string rs2=string(rs, 4);

What you are now doing is saying, "build me a std::string object, and I will help you do that by giving you this already existing std::string object and a number." The std::string class does something completely different in this case. When it receives those two arguments, it instead uses the number as place to start reading from the provided std::string.

These two different cases are two different constructors for the std::string class--they provide two different ways to build a std:string object based on the information you have available. You can easily make the second example work like the first by changing it to this form:

string rs2=string(rs, 0, 4);

Now you are telling it "initialize this new std::string (called rs2) by taking characters from the other std::string rs. I want you to start at the character at index 0 (so, the first) and take 4 characters after that--if they exist."

It is important to know, before you construct an object of a class, how the constructors behave. You can find documentation on all of the std::string constructors in many places online with some searches.

Author's note: It is noted that many other things are happening in this example (for example, the assignment operator) and many issues are glossed over, but given the self-stated beginner nature of this question, I felt it best to keep it high level.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.