N**********d 发帖数: 9292 | 1 源程序是:
#include
#include
using namespace std;
int main(int argc, char * argv[])
{
std::string m_ColumnName [] =
{
"str1",
"str2"
"last_one"
};
cout << m_ColumnName[0].substr(0,4) << endl;
cout << m_ColumnName[1].substr(0,4) << endl;
cout << m_ColumnName[2].substr(0,4) << endl;
return 0;
}
赋值的时候,"str2"后面少了个逗号,然后"last_one"到哪里去了?
是不是g++直接就把它扔了?
前两行输出都是预期的
str1
str2
而第三行则不确定,经常产生不同的值 | N**********d 发帖数: 9292 | 2 忘了说了,g++版本是4.4.5
【在 N**********d 的大作中提到】 : 源程序是: : #include : #include : using namespace std; : int main(int argc, char * argv[]) : { : std::string m_ColumnName [] = : { : "str1", : "str2"
| h*******s 发帖数: 8454 | 3 两个放在一起的字符串会自动被合并为一个字符串的吧
所以估计你那个str2后面就是last_one只是你没显示出来而已
【在 N**********d 的大作中提到】 : 源程序是: : #include : #include : using namespace std; : int main(int argc, char * argv[]) : { : std::string m_ColumnName [] = : { : "str1", : "str2"
| N**********d 发帖数: 9292 | 4 果然。。。
cout << m_ColumnName[1].substr(4,8) << endl;
输出就是last_one了
那为什么会这样合并呢?他们之间好像也没什么运算符呀?
【在 h*******s 的大作中提到】 : 两个放在一起的字符串会自动被合并为一个字符串的吧 : 所以估计你那个str2后面就是last_one只是你没显示出来而已
| t****t 发帖数: 6806 | 5 it's just c/c++ lexical convention
"a""b" means "ab"
strictly speaking, no literal newline can be included in a string literal (
except with \n), so for multiline string literal, such as
"a1\nb2\nc3\nd4\n"
it's more clear to write it as
"a1\n"
"b2\n"
"c3\n"
"d4\n"
of course you meant
"a1
b2
c3
d4
"
but newline is not allowed. c++0x introduced raw string literal which is a
little bit different.
【在 N**********d 的大作中提到】 : 果然。。。 : cout << m_ColumnName[1].substr(4,8) << endl; : 输出就是last_one了 : 那为什么会这样合并呢?他们之间好像也没什么运算符呀?
| N**********d 发帖数: 9292 | 6 啊,是这样呀。
谢谢啦。
【在 t****t 的大作中提到】 : it's just c/c++ lexical convention : "a""b" means "ab" : strictly speaking, no literal newline can be included in a string literal ( : except with \n), so for multiline string literal, such as : "a1\nb2\nc3\nd4\n" : it's more clear to write it as : "a1\n" : "b2\n" : "c3\n" : "d4\n"
|
|