问题描述
因此,我有以下代码,可以搜索一个向量中的对象的字符串。
#include< iostream>
#include< fstream>
#include< vector>
#include< algorithm>
#include< string>
struct migObj
{
migObj(const std :: string& a_name,const std :: string& a_location):name(a_name),location(a_location){}
std :: string name;
std :: string location;
};
int main()
{
typedef std :: vector< migObj> migVec;
migVec v;
v.push_back(migObj(fred,belfast));
v.push_back(migObj(ivor,london));
//按名称搜索。
const std :: string name_to_find =ivor;
auto i = std :: find_if(v.begin(),v.end(),[&](const migObj& obj){return name_to_find == obj.name;});
if(i!= v.end())
{
std :: cout< i-> name<< ,< i->位置<< \\\
;
}
return 0;
}
commecntname by name下面的代码负责此结果。我想做的是做一个方法添加到std :: vector或者typedef migVec,所以我可以调用
你不能这样做:C ++不有与C#中类似的扩展方法。
您可以从向量派生类,但是必须更改所有客户端代码。此外,正如Roddy指出的,std :: vector没有虚拟析构函数,所以从中推导出来是一个不好的想法。
函数会是更好的选择。额外的好处是,它也适用于大多数其他容器(例如 std :: list
),并且更加兼容STL中的大多数算法,这些算法通常也是自由功能。 / p>
所以你会有这样的:
myNewSearchFunction(v, ivor); // instead of v.myNewSearchFunction(ivor);
在内部,此函数 std :: find_if(v.begin ,v.end(),...
。
BTW,请注意,最好使用 std :: begin(v),std :: end(v)
比 v.begin(),v.end()
您可以在数组上运行相同的代码。
将事物放入类中。
So I have the following code that makes it possible to "search" for a string of an object in a vector.
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>
struct migObj
{
migObj(const std::string& a_name, const std::string& a_location) : name(a_name), location(a_location) {}
std::string name;
std::string location;
};
int main()
{
typedef std::vector<migObj> migVec;
migVec v;
v.push_back(migObj("fred", "belfast"));
v.push_back(migObj("ivor", "london"));
// Search by name.
const std::string name_to_find = "ivor";
auto i = std::find_if(v.begin(), v.end(), [&](const migObj& obj) { return name_to_find == obj.name;});
if (i != v.end())
{
std::cout << i->name << ", " << i->location << "\n";
}
return 0;
}
The code below the commecnt "search by name" is responsible for this result. What I want to do is make a method that is either added to the std::vector or to the typedef migVec, so I can call
v.myNewSearchFunction("ivor");
You can't do that: C++ doesn't have extension methods similar to what you have in C#.
You could derive your class from vector, but then you have to change all the client code. Besides, as Roddy points out, std::vector doesn't have a virtual destructor, so deriving from it is a bad idea in general.
IMO, writing a simple function would be much better choice. Additional benefit is that it would also work for most other containers (e.g. std::list
) and be more compatible with most of algorithms in STL which are also typically free functions.
So you would have this instead:
myNewSearchFunction(v, "ivor"); // instead of v.myNewSearchFunction("ivor");
Internally, this function does std::find_if(v.begin(), v.end(), ...
.
BTW, please note that it would be better to use std::begin(v), std::end(v)
than v.begin(), v.end()
. This lets you run the same code on e.g. arrays.
Putting things into classes isn't always the best choice in C++.
这篇关于向std :: vector或typdef添加自定义方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!