c++ - 使用 stringstream 代替字符串? - C++

标签 c++ string iostream

我的家庭作业是用 C++ 编写程序,但我们不允许使用字符串类。但是,我们可以使用 iostream库,包括 stringstream。我正在考虑使用 stringstream,我会使用 string 来构建我的类、从函数返回等。

这听起来是个好主意吗?有什么我应该注意的陷阱吗?

最佳答案

您也可以使用 vector。它是字符串的一个很好的替代品。正如 Scott Meyers 在他的“Effective STL”一书中所说:

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/

相关文章:

c++ - 表达式是在C++中调用函数的结果

c++ - 指向常量数组的指针

c++ - 我可以为在 std::map 中插入 2 对的函数提供强有力的保证吗?

我们可以在 C 中反转这个字符串吗?

java - 为什么按钮看起来很奇怪?

c++ - 使用字符串识别对象 : what's the purpose?

C++ 用户输入限制,正确重试,没有 "goto"

c++ - SDL C++ TTF 一段时间后随机消失(并且不会回来)

c++:将ostream存储在列表中

java - 在Java中从src文件夹打开文本文件时出错