题解 | #字符串链接#
字符串链接
https://www.nowcoder.com/practice/9f27c74ec91e4c7da96ba351dba235fc
//主要训练自己在vector和重载函数的编程能力,所以写得比较复杂 #include<iostream> #include<vector> const std::vector<char>& MyStrcat(std::vector<char>& dstStr,const std::vector<char>& srcStr) { for(char c : srcStr) dstStr.push_back(c); return dstStr; } std::istream& operator>>(std::istream& stream, std::vector<char>& s) { char c; while (stream.get(c) && c != ' ' && c != '\n') // 以空格或换行符作为两个字符串的分隔符 s.push_back(c); return stream; } std::ostream& operator<<(std::ostream& stream, const std::vector<char>& s) { for(char c : s) stream << c; return stream; } int main() { std::vector<char> s1; std::vector<char> s2; std::cin >> s1 >> s2; std::cout << MyStrcat(s1, s2) << std::endl; return 0; }