Код: Выделить всё
/*Сформировать строки таким образов, что бы первой была самая короткая строка, а последней самая длинная*/
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
using namespace std;
static inline bool compareLength (string const& lhs, string const& rhs)
{
return lhs.size() < rhs.size();
}
int main()
{
string str ("Lorem ipsum dolor sit amet");
string buffer;
stringstream ss (str);
vector<string> tokens;
while (ss >> buffer)
tokens.push_back(buffer);
vector<string>::iterator it = max_element(begin(tokens),end(tokens),compareLength);
string maxElement = *it;
it = min_element(std::begin(tokens), std::end(tokens), compareLength);
string minElement = *it;
vector<string>& vec = tokens;
vec.erase(std::remove(vec.begin(), vec.end(), maxElement), vec.end());
vec.erase(std::remove(vec.begin(), vec.end(), minElement), vec.end());
string output;
for (vector<string>::iterator it = vec.begin(); it != vec.end(); ++it)
{
output += *it + " ";
}
output.insert(output.length(), maxElement);
output.insert(0, minElement + " ");
cout << "Input:" << endl << str << endl << endl;
cout << "Output:" << endl << output << endl << endl;
return 0;
}
Код: Выделить всё
/*задан текст. выполнить сортировку слов расположив их в порядке увеличения букв*/
#include <vector>
#include <sstream>
#include <iterator>
#include <iostream>
#include <algorithm>
using namespace std;
bool op(const string &s1, const string &s2) { return s1.size() < s2.size(); }
int main() {
string s;
cout << "string? ";
getline(cin, s);
istringstream iss(s);
vector<string> v;
while (iss >> s) v.push_back(s);
sort(v.begin(), v.end(), op);
ostringstream oss;
copy(v.begin(), v.end(), ostream_iterator<string>(oss, " "));
s = oss.str();
cout << "result: " << s << endl;
}