C++,輸入一組數字,並對其升序排序,刪除指定範圍內的元素

#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
	string inputString;
	vector<string> sVec;
	cout<<"Please input the input string :";
	while (cin>>inputString) {
		sVec.push_back(inputString);
	}

	cout<<"The input string is : ";
	vector<string>::const_iterator firstIter = sVec.begin();    
	for (firstIter; firstIter != sVec.end(); ++firstIter) {
		cout<<*firstIter<<ends;
	}
	cout<<endl;

	cin.clear();


	//升序排序
	sort(sVec.begin(),sVec.end());
	cout<<"The after sort is ";
	vector<string>::const_iterator firstIter = sVec.begin();
	for (firstIter; firstIter != sVec.end(); ++firstIter) {
		cout<<*firstIter<<ends;
	}
	cout<<endl;

	cout<<"Please input the erease value:";
	string s1,s2;
	cin>>s1>>s2;

	vector<string>::iterator sIter = sVec.begin();
	vector<string>::iterator sIterEnd = sVec.end();
	vector<string>::iterator findIter,findIter2;
	findIter   = find(sIter,sIterEnd,s1);
	findIter2  = find(sIter,sIterEnd,s2); 
	if ((findIter != sIterEnd) && (findIter2 != sIterEnd)) {
		sVec.erase(findIter,findIter2);
	}

	cout<<"The output string is : ";
	vector<string>::const_iterator firstIter = sVec.begin();
	for (firstIter; firstIter != sVec.end(); ++firstIter) {
		cout<<*firstIter<<ends;
	}

	cout<<endl;

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章