我正在組織一個演示文稿,以展示我認爲C#可能提供的超過C++的一些生產力增益。我用C#編寫了一些代碼,我想將它轉換爲C++。我沒有時間或最新的C++做轉換正義,因此張貼在這裏。幫助 - 請將此LINQ示例轉換爲C++
代碼轉換:
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
namespace LinqTest
{
public class Vehicle
{
public int Id { get; set; }
public Color Colour { get; set; }
}
public class Tyre
{
public int Id { get; set; }
public int BikeId { get; set; }
public int Size { get; set; }
public string Brand { get; set; }
}
public class Car : Vehicle {}
public class Bike : Vehicle {}
public class Example
{
private readonly IList<Car> cars;
private readonly IList<Tyre> bikeTyres;
private readonly IList<Bike> bikes;
public Example()
{
cars = new List<Car>
{
new Car {Id = 0, Colour = Color.Red},
new Car {Id = 1, Colour = Color.Blue},
new Car {Id = 2, Colour = Color.Green},
new Car {Id = 3, Colour = Color.Green}
};
bikeTyres = new List<Tyre>
{
new Tyre {Id = 0, BikeId = 0, Brand = "TyreCo1", Size = 23},
new Tyre {Id = 1, BikeId = 0, Brand = "TyreCo1", Size = 23},
new Tyre {Id = 2, BikeId = 1, Brand = "TyreCo2", Size = 30},
new Tyre {Id = 3, BikeId = 1, Brand = "TyreCo2", Size = 30},
new Tyre {Id = 4, BikeId = 2, Brand = "TyreCo3", Size = 23},
new Tyre {Id = 5, BikeId = 2, Brand = "TyreCo3", Size = 23}
};
bikes = new List<Bike>
{
new Bike {Id = 0, Colour = Color.Red},
new Bike {Id = 1, Colour = Color.Blue},
new Bike {Id = 2, Colour = Color.Green}
};
}
public IEnumerable<Vehicle> FindVehiclesByColour(Color colour)
{
var carVehicles = from car in cars
where car.Colour == colour
select car as Vehicle;
var bikeVehicles = from bike in bikes
where bike.Colour == colour
select bike as Vehicle;
return carVehicles.Union(bikeVehicles);
}
public IEnumerable<Bike> FindBikesByTyreSize(int size)
{
return (from bike in bikes
join tyre in bikeTyres on bike.Id equals tyre.BikeId
where tyre.Size == size
select bike).Distinct();
}
}
}
在此先感謝。
我想添加一個賞金,因爲這個問題是相當複雜的,但似乎我不能馬上離開。 – ng5000 2009-07-06 09:46:02
爲什麼倒票?對我來說這似乎是一個合理的要求,我們當然會增加關於SO的知識。至少將我指向一些C++類型/方法。 – ng5000 2009-07-06 09:52:02