C++下string用法总结

本文介绍如何使用C++从文本文件中读取数据,并针对特定字段进行排序。通过实例演示了如何提取用户名并去除重复项,最终按字母顺序输出唯一的用户名。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考博文:

1)https://www.byvoid.com/blog/cpp-string



问题1)如果文本格式是:用户名 电话号码,文件名name.txtTom 23245332Jenny 22231231Heny 22183942Tom 23245332...

现在我们需要对用户名排序,且只输出不同的姓名。


参考网页:https://www.byvoid.com/blog/cpp-string


istream& getline (istream&  is, string& str, char delim);
其中:
is
istream object from which characters are extracted.
str
string object where the extracted line is stored.

代码如下:

ifstream in("name.txt");
string strtmp;
vector<string> vect;
while(getline(in,strtmp,'\n'))
	sort(vect.push_back(strtmp.substr(0,strtmp.fin(" "))));
vector<string>:: iterator it=unique(vect.begin(),vect.end());
copy(vect.begin,it,ostream_iterator<string>(cout,"\n"));


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值