Pass a dynamic array of objects to function

前端 未结 3 2038
说谎
说谎 2021-01-06 16:58

I am in the process of learning c++. So i know a method by which you send something to function and then work as if it was call by value but actually it is call by reference

3条回答
  •  萌比男神i
    2021-01-06 17:49

    Using std::vector you can also do the following:

    void list_cities(std::vector &cities)
    {
        for (size_t iCity = 0; iCity < cities.size(); iCity++){
            cout << cities[iCity].get_name() << endl;
        }
    }
    
    std::vector cities;
    // fill list here
    list_cities(cities);
    

提交回复
热议问题