我会为你这样做的,但这绝对是一个糟糕的“问题”。这更多的是一个工作请求,这就是为什么我的答案是社区维基。这里没有问题要回答。
#include <algorithm>
#include <list>
#include <string>
namespace LinqTest
{
// C++ does not specificy a standard GUI library.
// This is a small hack to make the code work below.
// In a real solution, one would probably have a real
// Color class that stores the red, green, and blue
// components of the colors and provides operations
// to act upon colors.
struct Color
{
static const int Red = 0;
static const int Green = 1;
static const int Blue = 2;
Color(void) :
value(0)
{
}
Color(int color) :
value(color)
{
}
bool operator==(const Color& rhs) const
{
return value == rhs.value;
}
int value;
};
struct Vehicle
{
Vehicle(void) :
Id(0)
{
}
Vehicle(int id, Color colour) :
Id(id),
Colour(colour)
{
}
bool operator==(const Vehicle& rhs) const
{
return Id == rhs.Id;
}
int Id;
Color Colour;
};
struct Tyre
{
Tyre(void) :
Id(0),
BikeId(0),
Size(0)
{
}
Tyre(int id, int bikeId, int size, std::string brand) :
Id(id),
BikeId(bikeId),
Size(size),
Brand(brand)
{
}
int Id;
int BikeId;
int Size;
std::string Brand;
};
struct Car :
public Vehicle
{
Car(void)
{
}
Car(int id, Color colour) :
Vehicle(id, colour)
{
}
};
struct Bike :
public Vehicle
{
Bike(int id, Color colour) :
Vehicle(id, colour)
{
}
};
class Example
{
// I put private up top to match yours, but most C++
// programmers would prefer it on the bottom, justified
// by the fact users of the class don't care or want
// to see how the class works, they want to see how to
// use it.
private:
std::list<Car> cars;
std::list<Tyre> bikeTyres;
std::list<Bike> bikes;
public:
Example(void)
{
cars.push_back(Car(0, Color::Red));
cars.push_back(Car(1, Color::Blue));
cars.push_back(Car(2, Color::Green));
cars.push_back(Car(3, Color::Green));
bikeTyres.push_back(Tyre(0, 0, 23, "TyreCo1"));
bikeTyres.push_back(Tyre(1, 0, 23, "TyreCo1"));
bikeTyres.push_back(Tyre(2, 1, 30, "TyreCo2"));
bikeTyres.push_back(Tyre(3, 1, 30, "TyreCo2"));
bikeTyres.push_back(Tyre(4, 2, 23, "TyreCo3"));
bikeTyres.push_back(Tyre(5, 2, 23, "TyreCo3"));
bikes.push_back(Bike(0, Color::Red));
bikes.push_back(Bike(1, Color::Blue));
bikes.push_back(Bike(2, Color::Green));
}
// I chose to return pointers to Vehicles to maintain any
// polymorphic behavior, since from what I understand C#
// would be returning references here.
std::list<Vehicle*> FindVehiclesByColour(Color colour)
{
typedef std::list<Car>::iterator car_iterator;
typedef std::list<Bike>::iterator bike_iterator;
std::list<Vehicle*> result;
for (car_iterator iter = cars.begin(); iter != cars.end(); ++iter)
{
if (iter->Colour == colour)
{
result.push_back(&(*iter));
}
}
for (bike_iterator iter = bikes.begin(); iter != bikes.end(); ++iter)
{
if (iter->Colour == colour)
{
result.push_back(&(*iter));
}
}
return result;
}
std::list<Bike*> FindBikesByTyreSize(int size)
{
typedef std::list<Tyre>::const_iterator tyre_iterator;
typedef std::list<Bike>::iterator bike_iterator;
std::list<Bike*> result;
for (tyre_iterator tyreIter = bikeTyres.begin(); tyreIter != bikeTyres.end(); ++tyreIter)
{
if (tyreIter->Size == size)
{
for (bike_iterator bikeIter = bikes.begin(); bikeIter != bikes.end(); ++bikeIter)
{
if (tyreIter->BikeId == bikeIter->Id)
{
result.push_back(&(*bikeIter));
}
}
}
}
result.sort();
result.unique();
return result;
}
};
}
注意这里有一些风格方面的东西,比如
Example(void)
对战
Example()
这是我自己的,并不一定代表其他C++程序员的风格。相关的,还有其他的方法,我的方法甚至可能不是最好的。
这里缺少注释,我会添加它们,但我认为它们只会妨碍我们的工作,代码非常简单,可以理解。
这都说,你显然对C++不太了解,所以说C是更有效率的,虽然它可能是真的,但是很难从你自己身上认真对待。此外,您在C中执行的一些算法实际上可能效率很低,比如只存储轮胎自行车的ID,然后对匹配的自行车进行线性搜索。