How to achieve such operation,the Visual Studio always tells me that it was wrong.
the wrong code is C2110 and E2140.
Can any one help?
string a="2323" "22323" "232332";
CodePudding user response:
The expression "2323"
is not a std::string
, it is a const char[5]
.
Since C 14, you can have a literal of type std::string
:
using namespace std::string_literals;
std::string a = "2323"s "22323"s "232332"s;
CodePudding user response:
There is no predefined operator
for const char[X]
(a const array of char, with X the number of characters in it; e.g. const char[5]
is the type of a literal "2323"
as in your code) - nor for const char *
, to which they could be automatically converted. That's why C compilers will not let you do this.
However, there is an operator
for std::string
. Since this operator in turn returns a string, you can chain-call, that is, call the operator again on its result.
So you can write something like this:
std::string a = std::string("2323") "22323" "232332";
In case you are already using C 14 or later, you can use literals of type string via an s
suffix, then an alternate form of writing this would be
using namespace std::string_literals;
std::string a = "2323"s "22323" "232332";
If it is just multiple const char[X]
literals that you want to concatenate, you can also just write them one after the other, like this:
std::string a("2323" "22323" "232332");
// or:
std::string a = "2323" "22323" "232332";
The compiler will automatically concatenate them for you.
CodePudding user response:
In this declaration
std::string a="2323" "22323" "232332";
you are using an expression with string literals and the operator that is not defined for character arrays and correspondingly for pointers to which the string literals are implicitly converted.
You could write for example
std::string a = "2323"s "22323" "232332";
using the user-defined string literal "2323"s
or you could write
std::string a = std::string( "2323" ) "22323" "232332";
Another way is to declare the object a
like
std::string a;
and then write
a = "2323";
a = "22323";
a = "232332";
Here is a demonstrative program.
#include <iostream>
#include <string>
int main()
{
{
using namespace std::literals;
std::string a = "2323"s "22323" "232332";
std::cout << a << '\n';
}
{
std::string a = std::string( "2323" ) "22323" "232332";
std::cout << a << '\n';
}
{
std::string a;
a = "2323";
a = "22323";
a = "232332";
std::cout << a << '\n';
}
return 0;
}
The program output is
232322323232332
232322323232332
232322323232332
CodePudding user response:
The "2323", "22323", "232332"
are not std::string
s rather string literals. There is no operator
defined for them to do such addition that you have done.
In c 17 however, using fold expression you can do:
#include <string>
using namespace std::string_literals;
template<typename... Args>
std::string add(const Args&... args)
{
return (""s ... args);
};
and now
std::string a = add("2323", "22323", "232332");
CodePudding user response:
In addition to the other answers provided, you can also use the std::stringstream
to build up an std::string
.
#include <sstream>
int main( )
{
std::stringstream ss{ };
ss << "2323" << "22323" << "232332";
// Maybe you also want to conditionally append something else.
if ( some_condition )
{
// Note that I'm inserting an integer here.
ss << 42;
}
// When you're ready you can call the str( ) member function
// to attain a copy of the underlying string.
std::string s{ ss.str( ) };
}