string长度计算问题

小利 2012-02-22 10:17:23
如题,下面是代码,为什么打印出来tmp.length()不是我想要的长度?


#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int
main()
{
string str = "";
string tmp = "";

ifstream in("test.txt");

while(getline(in, str))
{
cout<<str.length()<<" "<<str<<endl;

for(int i = 0; i < str.length(); i++)
{
cout<<std::hex<<(int)str[i]<<" ";
tmp.append(&str[i]);
}
cout<<endl<<tmp.length()<<"----"<<tmp<<"----"<<endl;
tmp = "";
}

in.close();

return 0;
}

...全文
100 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
JoeBlackzqq 2012-02-25
  • 打赏
  • 举报
回复
basic_string::append
basic_string& append(const E *s);
basic_string& append(const E *s, size_type n);
basic_string& append(const basic_string& str,
size_type pos, size_type n);
basic_string& append(const basic_string& str);
basic_string& append(size_type n, E c);
basic_string& append(const_iterator first, const_iterator last);
The member template function appends the operand sequence to the end of the sequence controlled by *this, then returns *this.

In this implementation, if a translator does not support member template functions, the template:

template<class InIt>
basic_string& append(InIt first, InIt last);
is replaced by:

basic_string& append(const_iterator first, const_iterator last);
See the related sample program.

JoeBlackzqq 2012-02-25
  • 打赏
  • 举报
回复

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
string str = "";
string tmp = "";

ifstream in("test.txt");

while(getline(in, str))
{
cout<<str.length()<<" "<<str<<endl;

for(int i = 0; i < str.length(); i++)
{
cout<<std::hex<<(int)str[i]<<" ";
tmp.append(&str[i], 1); // 添加一个字符(默认是添加字符串的)
}
cout<<std::dec; // 返回到十进制输出
//cout<<endl<<endl;
cout<<endl<<tmp.length()<<"----"<<tmp<<"----"<<endl<<endl;
tmp = "";
}

in.close();

return 0;
}

小利 2012-02-23
  • 打赏
  • 举报
回复
为什么要用buf?
不用不行么?
直接tmp.append(str[i])???

[Quote=引用 3 楼 visualeleven 的回复:]

你的tmp.append(&str[i]);这里不对
C/C++ code

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int
main()
{
string str = "";
……
[/Quote]
xs574924427 2012-02-23
  • 打赏
  • 举报
回复
你append的是地址
Qyee16 2012-02-23
  • 打赏
  • 举报
回复
tmp.append(&str[i]); 你传递的是地址哦。亲
Eleven 2012-02-22
  • 打赏
  • 举报
回复
你的tmp.append(&str[i]);这里不对

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int
main()
{
string str = "";
string tmp = "";

ifstream in("F:\\11.txt");
char buf[2] = {0};

while(getline(in, str))
{
cout<<str.length()<<" "<<str<<endl;

for(int i = 0; i < str.length(); i++)
{
cout<<std::hex<<(int)str[i]<<" ";
buf[0] = str[i];
tmp.append(buf);
}
cout<<endl<<tmp.length()<<"----"<<tmp<<"----"<<endl;
tmp = "";
}

in.close();

return 0;
}
ppsharp 2012-02-22
  • 打赏
  • 举报
回复
不是str.size()吗?
Jim_King_2000 2012-02-22
  • 打赏
  • 举报
回复
1. 是不是应该去掉取地址符? tmp.append(str[i]);
2. in.close()不需要显示调用,析够会关闭文件。

64,646

社区成员

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

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