我的家庭作业是用 C++ 编写程序,但我们不允许使用字符串类。但是,我们可以使用 iostream库,包括 stringstream。我正在考虑使用 stringstream,我会使用 string 来构建我的类、从函数返回等。
这听起来是个好主意吗?有什么我应该注意的陷阱吗?
最佳答案
您也可以使用 vector
Third, consider using a vector<char> instead of a string, vector implementations are not allowed to be reference counted, so hidden multithreading performance issues fail to arise. Of course, you forgo string's fancy member functions if you switch to vector<char>, but most of that functionality is available through STL algorithms anyway, so you're-not so much giving up functionality as you are trading one syntax for another.
我认为使用 stringstream 可能产生的主要问题是它不是严格意义上的容器。我不确定是否可以将算法和其他常规 STL 内容与流一起使用。
关于c++ - 使用 stringstream 代替字符串? - C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/933757/