Tag Archives: character string

[Solved] error: invalid operands of types ‘const char [6]‘ and ‘const char [6]‘ to binary ‘operator+‘

 

preface

When using strings in C++, we habitually use + to connect two strings enclosed in "". The error is reported: error: invalid operators of types' const char [6] 'and' const char [6] 'to binary' operator+',


1. Scenarios

	//std::string str = "hello" + "world"; // error: invalid operands of types 'const char [6]' and 'const char [6]' to binary 'operator+'
	//std::string str = std::string("hello") + " world"; // correct
	//std::string str = "hello" + std::string("world"); //correct
	std::string str = "hello"" world"; //correct

    cout << "str=" << str << endl;

When using the+operator to connect two strings wrapped with "", an error occurs. The reason is that in C++, the strings enclosed by "" are regarded as const char* types, rather than string types.

2. Solution

Refer to the explanation on stackoverflow. For details, please read error: invalid operators of types’ const char * ‘and’ const char * ‘to binary’ operator+’

1. Explicitly declare one of them as std::string type (recommended)

std::string str = std::string("hello") + " world";

2. Remove the+and let the compiler splice strings (not recommended)

std::string str = "hello"" world";

Most compilers automatically splice strings enclosed by "", but it is not guaranteed that all compilers are normal. It is recommended to display strings declared as string types and then perform the+operation.


3. Validation

	std::string str1 = std::string("hello") + " world" + "!"; // correct
    std::string str2 = "hello" + std::string(" world") + "!"; //correct
    std::string str3 = "hello"" world" "!"; //correct

    cout << "str1=" << str1 << endl;
    cout << "str2=" << str2 << endl;
    cout << "str3=" << str3 << endl;

Mybatis integrates Oracle query and reports an error in the datetime type field

Question:

The same SQL statement can be executed normally in Oracle, but an error will be reported in the mybatis framework: ora-01722: invalid number or string does not match the data type

solve

Convert variable to string type:

g.UPDATETIME >= TO_CHAR(TRUNC (SYSDATE)),
g.CHECKDATE >= TO_CHAR('2021-01-01 00:00:00'))

[Solved] TypeError: not all arguments converted during string formatting

When formatting the output of a string, one of the methods is as follows:

for value in ['A', 'B', 'C']:
    print('output: %s' % value)

When I print the running status of the sub process, print() I found several strings in the list, but the error is as follows:

TypeError: not all arguments converted during string formatting

Not all the parameters are converted during the string formatting period. I looked at the location of the error. It turned out that % was wrongly typed, and it became $, and I didn’t know the reason at first. I saw this error for the first time and sent this information to everyone for reference. This is a low-level mistake

Supplement: several ways of string output

Direct print() output

print('str1', 'str2', 'str3', '...')

'%s' % value Placeholder output

# 1. Single
>>> name = 'Jason'
>>> print('name: %s' % name)
name: Jason
# 2.Multiple
>>> name1, name2 = 'Jason', 'Bob'
>>> print('name1: %s; name2: %s' % (name1, name2))
name1:, Jason; name2: Bob
# Output order can also be customized
>>> print('name1: %s; name2: %s' % (name2, name1))
name1: Bob; name2: Jason

When outputting multiple string variables, multiple variables after % need to be enclosed in parentheses, otherwise the error of insufficient parameters will be reported

TypeError: not enough arguments for format string

format() format output

# 1. Single
>>> '{}'.format('world')
'world'
# 2. Multiple
>>> word1, word2 = 'hello', 'world'
>>> hello_world = '{}, {}'.format(word1, word2)
>>> hello_world
'hello, world'
# Output order can also be customized
>>> hello_world = '{1}, {1}'.format(word1, word2)
>>> hello_world
'world, world'

When output at specified position, all {} must have subscripts of parameters, otherwise it will not be able to convert from manual field number to automatic field number valueerror error:

ValueError: cannot switch from manual field specification to automatic field numbering

It must also be within the subscript range, otherwise an overrun indexerror error will be reported

IndexError: Replacement index 2 out of range for positional args tuple

In C + + cin.getline The difference between () and getline () functions

cin.getline ():
usage: receive a string, which can receive spaces and output. It needs to include & lt; CString & gt;

char m[20];
cin.getline(m,5);
cout<<m<<endl;

Input: jkljkljkl
output: jklj

Receive 5 characters into m, and the last one is’ \ 0 ‘, so only 4 characters can be output;

Extension:
1 cin.getline () actually has three parameters, cin.getline (variables of receiving string, number of receiving characters, end characters)
2. When the third parameter is omitted, the system defaults to ‘\ 0’
3 cin.getline () to read cin.getline When jlkjkljkl is input, jklj is output; when jkaljkljkl is input, JK is output

Getline():
usage: to receive a string, which can receive spaces and output. It needs to include & lt; CString & gt;

string str;
getline(cin,str);
cout<<str<<endl;

Input: jkljkljkl
output: jkljkljkl

Input: JKL jfksldfj jklsjfl
output: JKL jfksldfj jklsjfl