如何在C++;? 我开始从Python开始C++,所以我只是在浏览基本知识。当我尝试创建一个包含对象的数组时,会出现问题。在Python中,我将有一个具有属性color和year的类Car: myCars = [Car("Red", 1986), Car("Black", 2007), Car("Blue", 1993)] # and then going through the cars: for car in myCars: print("The car has the color " + car.color + " and is " + (2014 - car.year) + " years old.")

如何在C++;? 我开始从Python开始C++,所以我只是在浏览基本知识。当我尝试创建一个包含对象的数组时,会出现问题。在Python中,我将有一个具有属性color和year的类Car: myCars = [Car("Red", 1986), Car("Black", 2007), Car("Blue", 1993)] # and then going through the cars: for car in myCars: print("The car has the color " + car.color + " and is " + (2014 - car.year) + " years old."),python,c++,arrays,class,struct,Python,C++,Arrays,Class,Struct,尝试在C++中执行类似操作: struct Car { string color; int year; }; int cars[3] = {Car cars[0], Car cars[1], Car cars[2]} //EDIT: I wrote bilar but I meant cars. 但是,对这些汽车进行迭代确实很无趣,首先,这不起作用,其次,它们没有任何属性。我只是不明白,我想也许我错过了一些重要的东西,把它全搞错了,但是,我想我必须把它解释得干净利落。试试这个

尝试在C++中执行类似操作:

struct Car {
    string color;
    int year;
};

int cars[3] = {Car cars[0], Car cars[1], Car cars[2]}
//EDIT: I wrote bilar but I meant cars.
但是,对这些汽车进行迭代确实很无趣,首先,这不起作用,其次,它们没有任何属性。我只是不明白,我想也许我错过了一些重要的东西,把它全搞错了,但是,我想我必须把它解释得干净利落。

试试这个(C++11)-看起来几乎像Python:

std::vector<Car> cars = {{"Red", 1986}, {"Black", 2007}, {"Blue", 1993}};
for (const Car& car : cars) {
    std::cout << "The car has the color " << car.color << " and is " 
        << (2014 - car.year) << " years old." << std::endl;
}
std::vector cars={{“红色”,1986},{“黑色”,2007},{“蓝色”,1993};
用于(施工车辆和车辆:车辆){
std::cout试试这个(C++11)-看起来几乎像Python:

std::vector<Car> cars = {{"Red", 1986}, {"Black", 2007}, {"Blue", 1993}};
for (const Car& car : cars) {
    std::cout << "The car has the color " << car.color << " and is " 
        << (2014 - car.year) << " years old." << std::endl;
}
std::vector cars={{“红色”,1986},{“黑色”,2007},{“蓝色”,1993};
用于(施工车辆和车辆:车辆){

std::cout标准容器是可移植的,如果出于某种原因不能使用C++11语法(Anton Savin建议的语法),可以使用更详细的旧样式:

for (std::vector<Car>::const_iterator it=cars.begin(); it != cars.end(); ++it) {
    const Car & car = *it;
    std::cout << "The car has the color " << car.color << " and is " 
        << (2014 - car.year) << " years old." << std::endl;
}
for(std::vector::const_迭代器it=cars.begin();it!=cars.end();++it){
const Car&Car=*它;

std::cout标准容器是可移植的,如果出于某种原因不能使用C++11语法(Anton Savin建议的语法),可以使用更详细的旧样式:

for (std::vector<Car>::const_iterator it=cars.begin(); it != cars.end(); ++it) {
    const Car & car = *it;
    std::cout << "The car has the color " << car.color << " and is " 
        << (2014 - car.year) << " years old." << std::endl;
}
for(std::vector::const_迭代器it=cars.begin();it!=cars.end();++it){
const Car&Car=*它;

std::cout不必使用vector。您可以创建一个数组来代替std::vector

Car cars[] = {{"Red", 1986}, {"Black", 2007}, {"Blue", 1993}};
for (int i = 0; i < 3; ++i)
{
    std::cout << "The car has the color " << cars[i].color << " and is " 
        << (2014 - cars[i].year) << " years old." << std::endl;
}
carcars[]={{“红色”,1986},{“黑色”,2007},{“蓝色”,1993};
对于(int i=0;i<3;++i)
{

std::cout不必使用vector。您可以创建一个数组来代替std::vector

Car cars[] = {{"Red", 1986}, {"Black", 2007}, {"Blue", 1993}};
for (int i = 0; i < 3; ++i)
{
    std::cout << "The car has the color " << cars[i].color << " and is " 
        << (2014 - cars[i].year) << " years old." << std::endl;
}
carcars[]={{“红色”,1986},{“黑色”,2007},{“蓝色”,1993};
对于(int i=0;i<3;++i)
{

std::cout当你使用
int-bilar[3]
时,你正在创建一个整数数组。你需要使用
Car-bilar[3]
@IanAuld这听起来很合乎逻辑,我想知道类型……但是,如何给
Car[x]
赋值?我必须写
Car[2].color=“Red”< C++ >代码>在C++中,你可以在Python中创建构造函数,就像Python一样,你可以做<代码> Car MyAuCar = Car(‘红色’,‘2007’’);< /Cord>。我自己是Python的一个家伙,我的C++有点生锈,所以我不记得该怎么做。当你使用<代码> int BIALAR(3)时
您正在创建一个整数数组。您需要使用
Car bilar[3]
@IanAuld这听起来很合乎逻辑,我想知道类型……但是,如何为
Car[x]
赋值?我必须写
Car[2].color=“Red”在每个作业的一行上,你可以在C++中创建构造函数,就像Python一样,这样你就可以做<代码> Car MyAuCar = Car(‘红色’,‘2007’’)。我是一个Python的人,我的C++有点生疏,所以我不记得该怎么做。<代码> const <代码>是什么意思,这意味着我使用向量,这到底是什么意思?我也假设<<代码>:<代码> > < <代码> >代码> const <代码>,意思是<代码> CAR> <代码>应该是不可变的。一个
std::vector
应该基本上与Python列表没有区别。
const
是什么意思,这是否意味着我正在使用一个向量,这到底意味着什么?另外,我假设
中的
某种意思是
中的
const
意味着
car
应该是不可变的d::vector
应该基本上与Python列表没有区别。