C++学习十一

string类和标准模板库

string移动构造函数
string(string &&str);//新创建的string为str副本,但是与复制构造函数不同的是,他不保证将str视为const
string输入
//c风格字符串输入
char info[100];
cin>>info;
cin.getline(info,100);
cin.get(info,100);
//string对象风格
string stuff;
cin>>stuff;
getline(cin,stuff);

两个版本的getline()都有一个可选参数,用于确定输入边界

cin.getline(info,100,':');//read up to':',discard ':'
getline(info,':');//read up to ':',discard ':'
从文件中读取string
#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>
int main()
{
   
	using namespace std;
	ifstream fin;
	fin.open("tobuy.txt");
	if(fin.is_open()==false)
	{
   
		cerr<<"can't open file.Bye.\n";
		exit(EXIT_FAILURE);
	}
	string item;
	int count=0;
	getline(fin,item,':');
	while(fin)// if input is good
	{
   
		++count;
		cout<<count<<":"<<item<<endl;
		getline(fin,item,':');
	}
	cout<<"Done\n";
	fin.close();
	return 0;
}

通常程序要查找的文本文件,应将其放在可执行程序或项目文件所在目录中,否则要给出完整路径名

fin.open("C:\\CPP\\Progs\\tobuy.txt");//加个转义字符
find方法

在这里插入图片描述
在这里插入图片描述

find_first_of方法
int where=snake1.find_first_of("hark");//"hark"各个字符在“shark”中第一次出现的位置
int where=snake1.find_last_of("hark");//最后一次出现
int where=snake1.find_first_not_of
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值