cout string输出空字符串???

loenepndulm 2008-11-26 02:36:32
代码是这样的,可以通过编译,但为什么我的temp输出为空字符串, 我一个个的输出都有内容的.

#include <iostream>
#include <string>
#include <cctype>
using std::string;
using std::cin;
using std::cout;
using std::endl;
int main() {
string str, temp;
cin >> str;
string::size_type index, j;
for(index = 0, j = 0; index < str.size(); ++index) {
if(!ispunct(str[index])) {
temp[j] = str[index];
++j;
//cout << str[index] << " " << j << endl;
}
}
for(int i = 0; i<3; ++i) {
cout << temp[i] << endl;
}
cout << temp << "ok" << endl;
//str = temp;
//cout << str << endl;
return 0;

}
...全文
557 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
fibbery 2008-11-26
  • 打赏
  • 举报
回复
也许吧,push_back在VC6中是不兼容的。
loenepndulm 2008-11-26
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 fibbery 的回复:]
temp[j] = str[index];
这样的操作是不正确的,因为你现在不知道temp字符串中的内存是多大的空间,需要使用temp.push_back(c)函数或者temp+=c来操作。
C/C++ code
#include <iostream>
#include <string>
#include <cctype>
using std::string;
using std::cin;
using std::cout;
using std::endl;
int main()
{
string str, temp;
cin >> str;
string::const_iterator index=str.begin()…
[/Quote]

谢谢你, 可以结贴早了, 或许最合适的就是用temp+=c来插入新字符.
fibbery 2008-11-26
  • 打赏
  • 举报
回复
楼主真是好孩子,结贴真快!:)
fibbery 2008-11-26
  • 打赏
  • 举报
回复
temp[j] = str[index];
这样的操作是不正确的,因为你现在不知道temp字符串中的内存是多大的空间,需要使用temp.push_back(c)函数或者temp+=c来操作。

#include <iostream>
#include <string>
#include <cctype>
using std::string;
using std::cin;
using std::cout;
using std::endl;
int main()
{
string str, temp;
cin >> str;
string::const_iterator index=str.begin();
for(; index !=str.end(); ++index)
if(!ispunct(*index))
temp.push_back(*index);

for(int i = 0; i<3; ++i)
cout << temp[i] << endl;
cout << temp << "ok" << endl;
//str = temp;
//cout << str << endl;
return 0;
}
wudeshou82666 2008-11-26
  • 打赏
  • 举报
回复
用堆上的内存吧

#include <iostream>
#include <string>
#include <cctype>
using std::string;
using std::cin;
using std::cout;
using std::endl;
int main() {
string str;
cin >> str;
string::size_type index, j;
char *temp=new char[str.size()+1];
for(index = 0, j = 0; index < str.size(); ++index) {
if(!ispunct(str[index])) {
temp[j] = str[index];
++j;
//cout << str[index] << " " << j << endl;
}
}
temp[j]='\0';
for(int i = 0; i<3; ++i) {
cout << temp[i] << endl;
}
cout << temp << "ok" << endl;
delete temp;
temp=NULL;
//str = temp;
//cout << str << endl;
return 0;

}
loenepndulm 2008-11-26
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 hai040 的回复:]
temp[j] = str[index];
换成
temp.push_back(str[index]);
[/Quote]
按你的方法成功了, 分给你吧.
最后得出的结论是:C++的类机制本来是要方便人的, 但往往更麻烦.
wudeshou82666 2008-11-26
  • 打赏
  • 举报
回复
temp.push_back(str[index]);
string 没有这个成员函数吧。。。。。。。
self_control 2008-11-26
  • 打赏
  • 举报
回复
为什么是越界了?
他不是说一个个输出都可以?
loenepndulm 2008-11-26
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 hai040 的回复:]
temp[j]越界了,temp.size()还是0
[/Quote]
如果我越界了那在string里是如何插入一个新字符呢?
loenepndulm 2008-11-26
  • 打赏
  • 举报
回复
我想把输入字符串的符号去掉而已.
wudeshou82666 2008-11-26
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 hai040 的回复:]
temp[j]越界了,temp.size()还是0
[/Quote]
确实!你这时候的temp[0]是不存在的,所以会报内存错误
你用数组吧
呵呵
hai040 2008-11-26
  • 打赏
  • 举报
回复
temp[j] = str[index];
换成
temp.push_back(str[index]);
self_control 2008-11-26
  • 打赏
  • 举报
回复
会不会是因为没结束符啊,字符是可以=赋值的

string当char[]用干嘛?
liqiang_2008 2008-11-26
  • 打赏
  • 举报
回复
你想做什么呢?
其实你的代码头部只需using namespace std;就可以代替以下几行:
using std::string;
using std::cin;
using std::cout;
using std::endl;

还是那句,你说说你的程序想做什么?
fllowrain 2008-11-26
  • 打赏
  • 举报
回复
temp[j] = str[index];
字符是不是不能直接用“=”赋值呀?
hai040 2008-11-26
  • 打赏
  • 举报
回复
temp[j]越界了,temp.size()还是0
lsldd 2008-11-26
  • 打赏
  • 举报
回复
temp没有'\0'结束符.

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧