string 类型字符串无法保存到 list 中

CusterFun 2014-12-10 04:15:25
在线等,急,谢谢各位大神
问题是:输入字符串到list中之后只能输出首个字符,不能输出整个字符串
截图和代码如下:


代码:
//const char *
#include <iostream>
#include <list>
#include <string>
using namespace std;
using std::list;
using std::string;
template<typename iterator>
void print(iterator begin,iterator end){
while(begin!=end)
cout<<*begin++<<' ';
cout<<endl;
}
int main(void)
{
for(;;){
cout<<"请字符串:"<<endl;
string str;
cin >> str;
const char *codename = str.c_str();
list <char> list;
list.push_back(*codename);
print(list.begin(),list.end());}
system("PAUSE");
return 0;
}
...全文
279 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
我看你有戏 2014-12-12
  • 打赏
  • 举报
回复


#include "iostream"
#include "string"
#include "list"
#include "vector"
using namespace std;

template<typename iterator>
void print(iterator begin,iterator end)
{
	while(begin!=end)
	{
		cout<<*begin++<<" ";
	}
	cout<<endl;

}
void main()
{
	for (;;)
	{
		cout<<"输入字符串:"<<endl;
		string str;
		cin>>str;
		vector<char> myvector;
		const char* p =str.c_str();
		myvector.assign(str.c_str(),str.c_str()+str.size());
		print(myvector.begin(),myvector.end());

	}
	system("pause");
}
我看你有戏 2014-12-12
  • 打赏
  • 举报
回复

#include "iostream"
#include "string"
#include "list"
using namespace std;

template<typename iterator>
void print(iterator begin,iterator end)
{
	while(begin!=end)
	{
		cout<<*begin++<<" ";
	}
	cout<<endl;

}
void main()
{
	for (;;)
	{
		cout<<"输入字符串:"<<endl;
		string str;
		cin>>str;
		list<char> mylist;
		const char* p =str.c_str();
		for(int i=0;i<str.size();i++)
			mylist.push_back(p[i]);

		print(mylist.begin(),mylist.end());

	}
	system("pause");
}
CusterFun 2014-12-12
  • 打赏
  • 举报
回复
引用 15 楼 henry3695 的回复:


#include "iostream"
#include "string"
#include "list"
#include "vector"
using namespace std;

template<typename iterator>
void print(iterator begin,iterator end)
{
	while(begin!=end)
	{
		cout<<*begin++<<" ";
	}
	cout<<endl;

}
void main()
{
	for (;;)
	{
		cout<<"输入字符串:"<<endl;
		string str;
		cin>>str;
		vector<char> myvector;
		const char* p =str.c_str();
		myvector.assign(str.c_str(),str.c_str()+str.size());
		print(myvector.begin(),myvector.end());

	}
	system("pause");
}
谢谢你,我按照楼上的list<string> list解决了,谢谢你的帮助
yangyunzhao 2014-12-11
  • 打赏
  • 举报
回复
楼主犯了一个错误,string的c_str(),是不能直接保存的。你读读源码,就知道为何不能直接保存了
CusterFun 2014-12-10
  • 打赏
  • 举报
回复
引用 11 楼 starytx 的回复:
push_back(str)或者 push_back(codename) 效果都是一样的,都是存入了一个string
谢谢你,我试一下,你真是好人呀
starytx 2014-12-10
  • 打赏
  • 举报
回复
push_back(str)或者
push_back(codename)
效果都是一样的,都是存入了一个string
CusterFun 2014-12-10
  • 打赏
  • 举报
回复
引用 9 楼 starytx 的回复:
list<string>直接push_back一个string进去不就完了,为啥费劲一个一个存单个字符
不行的呀e:\visual studio 2010projects\lts\ltstest\test\const char test\const char test.cpp(23): error C2664: “void std::list<_Ty>::push_back(_Ty &&)”: 不能将参数 1 从“const char”转换为“std::string &&” 1> with 1> [ 1> _Ty=std::string 1> ] 1> 原因如下: 无法从“const char”转换为“std::string” 1> 无构造函数可以接受源类型,或构造函数重载决策不明确 1> 我需要 把const char *类型 存入list中
starytx 2014-12-10
  • 打赏
  • 举报
回复
list<string>直接push_back一个string进去不就完了,为啥费劲一个一个存单个字符
CusterFun 2014-12-10
  • 打赏
  • 举报
回复
引用 1 楼 starytx 的回复:
你的list就push_back了一个字符,怎么能输出正常呢
int main(void) { for(;;){ cout<<"请字符串:"<<endl; string str; cin >> str; const char *codename = str.c_str(); list <char> list; char *a[]=*codename; list.push_back(*a); print(list.begin(),list.end());} system("PAUSE"); return 0; } 如果把codename这个指针,存到指针数组中,再把数组存到list中可以吗?怎么实现呀
CusterFun 2014-12-10
  • 打赏
  • 举报
回复
引用 5 楼 henry3695 的回复:
list<string> test it
这个不行的 错误如下:e:\visual studio 2010projects\lts\ltstest\test\const char test\const char test.cpp(23): error C2664: “void std::list<_Ty>::push_back(_Ty &&)”: 不能将参数 1 从“const char”转换为“std::string &&” 1> with 1> [ 1> _Ty=std::string 1> ] 1> 原因如下: 无法从“const char”转换为“std::string” 1> 无构造函数可以接受源类型,或构造函数重载决策不明确 1>
CusterFun 2014-12-10
  • 打赏
  • 举报
回复
引用 1 楼 starytx 的回复:
你的list就push_back了一个字符,怎么能输出正常呢
或者不用 list 有什么好的方法没有,
我看你有戏 2014-12-10
  • 打赏
  • 举报
回复
list<string> test it
CusterFun 2014-12-10
  • 打赏
  • 举报
回复
引用 1 楼 starytx 的回复:
你的list就push_back了一个字符,怎么能输出正常呢
那该怎么压入才能输出整个字符串呀
CusterFun 2014-12-10
  • 打赏
  • 举报
回复
那该怎么压入才能输出整个字符串呀
CusterFun 2014-12-10
  • 打赏
  • 举报
回复
那该怎么压入才能输出整个字符串呀
starytx 2014-12-10
  • 打赏
  • 举报
回复
你的list就push_back了一个字符,怎么能输出正常呢

64,637

社区成员

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

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