由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 问个缺少逗号的数组赋值问题
相关主题
问个char * 的问题which str_cmp code is better?
reverse words, not the Microsoft one!!!请帮忙看一个python string comparion的问题,有包子.
这个程序怎么解决grep或egrep怎样输出匹配到的不连续的字符串?
问个指针array 的简单问题js: 如何确认st1是str2+" \ 0 \ 0 \ 0 " ?
[合集] 一个依赖于平台的memory问题帮看看这个C++里trim last blanks,错在哪里?
c/c++/java的对象/结构输入请教一道入门小题
请教如何使用qsort() to sort string.这个有更好的算法吗?
求改进小函数python 小问题
相关话题的讨论汇总
话题: columnname话题: substr话题: str2话题: endl话题: cout
进入Programming版参与讨论
1 (共1页)
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"

1 (共1页)
进入Programming版参与讨论
相关主题
python 小问题[合集] 一个依赖于平台的memory问题
c++ questionc/c++/java的对象/结构输入
我也问个UDP网络编程中遇到的问题请教如何使用qsort() to sort string.
也问个regular expression的问题求改进小函数
问个char * 的问题which str_cmp code is better?
reverse words, not the Microsoft one!!!请帮忙看一个python string comparion的问题,有包子.
这个程序怎么解决grep或egrep怎样输出匹配到的不连续的字符串?
问个指针array 的简单问题js: 如何确认st1是str2+" \ 0 \ 0 \ 0 " ?
相关话题的讨论汇总
话题: columnname话题: substr话题: str2话题: endl话题: cout