c++关于字符串的读入和截取
#include<iostream>
#include<string>
#include<vector>
using namespace std;
vector<string> splitEx(const string& src, string separate_character)
{
	vector<string> strs;
	int separate_characterLen = separate_character.size();//分割字符串的长度,这样就可以支持如“,,”多字符串的分隔符
	int lastPosition = 0, index = -1;
	while (-1 != (index = src.find(separate_character, lastPosition)))
	{
		strs.push_back(src.substr(lastPosition, index - lastPosition));
		lastPosition = index + separate_characterLen;
	}
	string lastString = src.substr(lastPosition);//截取最后一个分隔符后的内容
	if (!lastString.empty())
		strs.push_back(lastString);//如果最后一个分隔符后还有内容就入队
	return strs;
}
int main()
{
	/*
	string s;
	string  sum;
	while (cin >> s){
		sum += s + " ";
		if (cin.get() == '\n')
			break;
	}
	cout << sum << endl;
	*/
	int tpp = 1;
	string str;
	do{
		string s;
		string temp;
		cout << "输入一行字符串:\n";
		while (cin >> s){
			temp += s + " ";
			if (cin.get() == '\n')
				break;
		}
		cout << "继续输入请按1";
		cout << temp << endl;
		str = temp;
		cin >> tpp;
	} while (tpp==1);
	
	//string str;
	/*
	int count;//单词个数
	int i, j;
	*/
	//cout << "输入一行字符串:\n";
	//getline(cin, str);
	/*
	for (count = 0, j = str.size(), i = 0; i<j;)
	{
	while (str[i] == ' '&&i<j) i++;
	if (i<j)  count++;
	while (str[i] != ' '&&i<j) i++;
	}
	cout << "j=" << str.size() << endl;
	cout << "输入的字符串为:\n" << str << endl;
	cout << "字符串中包含的单词数为:" << count << endl;
	*/
	
	string del = " ";
	vector<string> strs = splitEx(str, del);
	//cout << "strsSize=" << strs.size() << endl;
	cout << "输入的字符串为:\n" << str << endl;
	cout << "字符串中包含的单词数为:" << strs.size() << endl;
	//int a,b,c,d;
	unsigned int i = strs.size()-1;
	while (i >=0)
	//for (unsigned int i = 0; i < strs.size(); i++)
	{
		int j = 1;
	int a = atoi(strs[i].c_str());
	int b = atoi(strs[i-1].c_str());
	int c = atoi(strs[i - 2].c_str());
	int d = atoi(strs[i-3].c_str());
	int cc = atoi(strs[j+2].c_str());
	//cout <<b+1 << endl;
	if (a == b &&  c == d){
	cout << "yes" << endl;
	break;
	}
	else
	{
	cout << "no" << endl;
	break;
	}
	//i -= 3;
	}
	
	
	cout << endl;
	system("pause");
	return 0;
}
 
                    
                     
                    
                 
                    
                 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号