Это программа выводит последовательно выражения между пробелами, то есть здесь выведет "abrb ps".
Код | #include <iostream> #include <string> #include <sstream>
int main(int argc, char* argv[]) { std::string str("abr abrb abr ps abv"); std::stringstream iter(str); while (true) { std::string buf; std::getline(iter, buf, ' '); buf.clear(); std::getline(iter, buf, ' '); if ( iter.eof() ) break; std::cout << buf << ' '; } std::cin.sync(); std::cin.get(); return 0; }
|
или так:
Код | #include <iostream> #include <string>
class between_spaces { private: bool was_space; public: bool operator() (char sym) { if (sym == ' ') was_space = !was_space; if (sym != ' ' && was_space) return true; return false; } between_spaces():was_space(false){} };
template <typename PRED, typename InIter, typename OutIter> void copy_if(InIter FIRST, InIter LAST, OutIter OUT, PRED Cond) { for ( ; FIRST != LAST; ++FIRST) if ( Cond(*FIRST) ) OUT = *FIRST; }
int main(int argc, char* argv[]) { std::string str("abr abrb abr ps gr"); copy_if( str.begin(), str.end(), std::ostream_iterator<char> (std::cout), between_spaces() ); std::cin.sync(); std::cin.get(); return 0; }
|
Если нужно ТОЛЬКО между первым и вторым пробелами, то
Код | #include <iostream> #include <string> #include <sstream>
int main(int argc, char* argv[]) { std::string str("abr abrb abr ps abv"); std::stringstream iter(str); std::string buf; std::getline(iter, buf, ' '); buf.clear(); std::getline(iter, buf, ' '); std::cout << buf << ' '; std::cin.sync(); std::cin.get(); return 0; }
|
|