[英]Accessing element of an array of struct.
So I have an struct 所以我有一个结构
struct car{
string ownerName;
float price;
int year;
};
and I declared an array of these structs car *cars = new car[1000] Each car has an index, for example, the car with index 0 has name John Smith. 我声明了这些结构的数组car * cars = new car [1000]每辆车都有一个索引,例如,索引为0的汽车的名称为John Smith。
So, my question is knowing the name of the owner how do I access the index of the car. 因此,我的问题是知道车主的姓名,我如何访问汽车的索引。 I know that the other way i would write cars[0].name, to get the name, but how would i do it backwards? 我知道我会以其他方式写cars [0] .name来获得名称,但是我将如何向后做呢?
Two possible ways come to my mind. 我想到两种可能的方法。 One is writing a function that finds index by name. 一种是编写一种按名称查找索引的函数。
#include <string>
using namespace std;
car *find_by_name(car* cars, const string& name, int from, int to) {
car* car_ptr = NULL;
for(int i = from; i < to; i++) {
if (cars[i].ownerName == name) {
car_ptr = cars+i;
break;
}
}
return car_ptr;
}
As you may notice, this function is very expensive (O(n)). 您可能会注意到,此函数非常昂贵(O(n))。 The other one and the easiest one, in my opinion, is using Map
or HashMap
to do so. 我认为,另一种也是最简单的一种是使用Map
或HashMap
来实现的。
#include <map>
#include <string>
#include <iostream>
using namespace std;
car set_new_val(map<string, car*>& my_map, const string& name, const float price, const int year) {
car* car_heap = new car();
car_heap->ownerName = name;
car_heap->price = price;
car_hep->year = year;
my_map.insert(pair<string, car*>(name, car_heap));
}
car* find_car_by_name(const map<string, car*>& my_map, const string& name) {
map<string, car*>::iterator it;
if ((it = my_map.find(name)) == my_map.end())
return NULL;
return it->second;
}
int main(int argc, char* argv[]) {
map<string, car*> my_cars_data;
set_new_val(my_cars_data, "James", 2233000.5, 2013);
set_new_val(my_cars_data, "Dave", 1222000.5, 2011);
set_new_val(my_cars_data, "Aaron", 1222000.75, 2012);
car* james_car = find_car_by_name(my_cars_data, "James");
cout << "Year " << james_car->year << endl;
car* null_car = find_car_by_name(my_cars_data, "Santa");
if (null_car == NULL)
cout << "No owner with the name Santa is recorded" << endl;
...
...
free_map(my_map);
return 0;
According to C++11, lookup for a key using Map
takes O(lgn) ( HashMap
is O(1)), for more details read here . 根据C ++ 11,使用Map
查找键的开销为O(lgn)( HashMap
为O(1)),有关更多详细信息,请参见此处 。 That's a big pro, if you handle mass of data (not to mention that it is easier to maintain). 如果您处理大量数据(更不用说它更易于维护),那将是一个很大的优势。
If you use a sequential container (array, vector...) you have to search for the name. 如果使用顺序容器(数组,向量...),则必须搜索名称。 In an unsorted array a linear search is required. 在未排序的数组中,需要线性搜索。
// linear search
string name = "joe";
auto it = find_if(begin(cars), end(cars),
[&name](const car& c) { return c.ownerName == name; });
auto index = it - begin(cars);
If you have performance problems with this approach you could sort the array and use a binary search (preferable if your array of cars does not change) or use a associative container which gives you fast access to an element by key (map, multi_map, unordered_map...) 如果您在使用此方法时遇到性能问题,则可以对数组进行排序并使用二进制搜索(如果您的汽车数组不变,则最好使用该数组)或使用关联容器,该容器可让您通过键(map,multi_map,unordered_map)快速访问元素...)
// binary search
struct {
bool operator()(const car& lh, const car& rh) { return lh.ownerName < rh.ownerName; };
bool operator()(const car& lh, const std::string& rh) { return lh.ownerName < rh; };
bool operator()(const std::string& lh, const car& rh) { return lh < rh.ownerName; };
} byName;
sort(begin(cars), end(cars), byName);
auto it2 = lower_bound(begin(cars), end(cars), name, byName);
if (it != end(cars) && it->ownerName == name)
{
auto index2 = it - begin(cars);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.