C ++ string management

Why does the following code have a compilation error?

#include<iostream>
#include<string>
using namespace std;

int main()
{
    string str="abc";

    string result=str[0];
    cout<<result<<endl;
    return 0;
}

      

However, the following code works fine:

#include<iostream>
#include<string>
using namespace std;

int main()
{
    string str="abc";

    str=str[0];
    cout<<str<<endl;
    return 0;
}

      

I am working on unix and compilation team: "g ++ -g test.cpp -std = c ++ 11 -o a", thenm./a

Error for the first test.cpp after compilation:

test.cpp:9:21: error: invalid conversion from 'char' to 'const char*' [-fpermissive]
  string result=str[0];
                     ^
In file included from /usr/um/gcc-4.8.2/include/c++/4.8.2/string:52:0,
                 from /usr/um/gcc-4.8.2/include/c++/4.8.2/bits/locale_classes.h:40,
                 from /usr/um/gcc-4.8.2/include/c++/4.8.2/bits/ios_base.h:41,
                 from /usr/um/gcc-4.8.2/include/c++/4.8.2/ios:42,
                 from /usr/um/gcc-4.8.2/include/c++/4.8.2/ostream:38,
                 from /usr/um/gcc-4.8.2/include/c++/4.8.2/iostream:39,
                 from test.cpp:1:
/usr/um/gcc-4.8.2/include/c++/4.8.2/bits/basic_string.h:490:7: error:   initializing argument 1 of 'std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]' [-fpermissive]
       basic_string(const _CharT* __s, const _Alloc& __a = _Alloc());
       ^

      

+3


source to share


3 answers


std::basic_string

doesn't have any constructor that takes a single argument CharT

. This means that std::string

(i.e. std::basic_string<char>

) cannot be built from one char

.

However, the class has an assignment operator overload that takes one argument CharT

, so your second example compiles.

The difference between the two is that you are initializing the copy first, which means that technically you first try to create a temporary instance std::string

from the argument char

and then copy it to result

. In the second, you perform an assignment, which means assigning a new value to an existing instance std::string

.




basic_string

has a constructor that takes a counter followed by a character:

basic_string(size_type count, CharT ch, const Allocator& alloc = Allocator());

      

so that your original example compiles if you change the offending line to

string result = {1, str[0]};

      

+3


source


The following code works fine:

string result;
result=str[0];

      

This means that the difference is between initialization and simple assignment and if you look at the error:

error: invalid conversion fromchar’ to ‘const char*’

      



it should be clear that initialization is not "full featured" as an assignment - the no constructor string

that takes an argument char

(there is an assignment that takes char

, so your second example works).

You can fix this (in one case, no doubt others) by providing initialization with a string and not a symbol:

string result = str.substr(0,1);

      

+1


source


str [0] returns char &, but no conversion from char & to std :: string

try instead

string result = string(1, str[0]);

      

0


source







All Articles