#include <iostream>
#include <string>
#include <vector>
template <typename T, typename FuncT>
static void createElements(const std::vector<T>& v, FuncT create)
{
for (const auto& item : v)
{
create(item);
}
}
class edge {
public:
edge() {
street_names = {"gogogo"};
}
std::vector<std::string> streetNames() const
{
return street_names;
}
std::vector<std::string> street_names;
};
class ODSegment {
public:
ODSegment(){
}
std::vector<std::string> street_names; /**< the names of street. */
};
int main(int argc, char **argv) {
std::shared_ptr<ODSegment> segment = std::make_shared<ODSegment>();
std::vector<std::string> v_str;
std::vector<std::string> mystr;
edge e;
auto test1 = e.streetNames().begin();
auto test2 = e.streetNames().end();
segment->street_names.assign(test1, test2);
auto v1 = e.streetNames();
auto v2 = e.streetNames();
std::cout << "v1 begin: " << &(*v1.begin()) << std::endl;
std::cout << "v1 begin: " << &(*v1.end()) << std::endl;
std::cout << "v2 end: " << &(*v2.begin()) << std::endl;
std::cout << "v2 end: " << &(*v2.end()) << std::endl;
// segment->street_names.assign(e.streetNames().begin(), e.streetNames().end()); // error!!!
createElements(segment->street_names, [&](const std::string& name) {
std::cout << name << "~~~" << std::endl;
});
return 1;
}
输出:
v1 begin: 0x7fc8aa405b20
v1 begin: 0x7fc8aa405b38
v2 end: 0x7fc8aa405b40
v2 end: 0x7fc8aa405b58
gogogo~~~
通过上面v1,v2的内存地址可以看出(e.streetNames().begin(), e.streetNames().end())是不同的迭代器,所以v1.begin() 不能匹配到 v2.end()