题解 | #句子逆序#
句子逆序
https://www.nowcoder.com/practice/48b3cb4e3c694d9da5526e6255bb73c3
学习和记录一下
方法1:
#include <iostream> #include <vector> #include <string> using namespace std; int main() { string s; vector<string> v; while (cin >> s) { v.push_back(s); } for (auto it = v.rbegin(); it != v.rend(); it++) { cout << *it << " "; } }
方法2
#include <iostream> #include <vector> #include <string> #include<bits/stdc++.h> using namespace std; int main() { string s; vector<string> v; while (cin >> s) { v.emplace_back(s); } copy(v.rbegin(), v.rend(), ostream_iterator<string> (cout, " ")); }
方法3
#include <iostream> #include <string> #include <stack> using namespace std; int main() { string str; stack<string> sta; while (cin >> str) { sta.push(str); } while (!sta.empty()) { cout << sta.top() << " "; sta.pop(); } }