“没有匹配的成员函数push_back”
在 Bjarne 的原始版本中,C 的 vector 是这样写的
vector<Value_type<C>*> res;
但是这个 Value_type 模板不能正常工作 所以我只是用 C* 指针替换它,仍然没有帮助
#include <vector>
#include <string>
#include <iostream>
using namespace std;
template <typename C, typename V>
vector<C*> find_all(C& c, V v) {
vector<C*> res;
for (auto &x : c) {
if (x==v)
res.push_back(&x);
}
return res;
}
int main() {
string s = "werqewreqwreqw";
for (const auto p : find_all(s,'q')) {
cout << p;
}
}
最佳答案
C*
与 C::value_type*
不同.实际上,已经有一个表示相同概念的typedef,即C::pointer
。 .
template <typename C, typename V>
vector<typename C::pointer> find_all(C& c, V v) {
vector<typename C::pointer> res;
仅供引用,C::pointer
将是 std::allocator_traits<T>::pointer
,这也是 T*
对于默认分配器或 value_type*
.我不完全确定为什么 Value_type
被使用了,但我假设这是 Bjarne 的想法,用于替换 typename C::value_type
的元函数.
关于c++ - Templates::Stroustrup 的示例未编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32730100/