如何从一个 list<int>初始化一个 vector<double>?从一个vector<int>又该如何创建?编写代码验证你的答案。
时间: 2023-05-11 11:04:04 浏览: 69
可以使用vector的构造函数来从list中初始化vector,代码如下:
```c++
#include <iostream>
#include <vector>
#include <list>
int main() {
std::list<int> l = {1, 2, 3, 4, 5};
std::vector<double> v(l.begin(), l.end());
for (auto i : v) {
std::cout << i << " ";
}
std::cout << std::endl;
std::vector<int> v2 = {1, 2, 3, 4, 5};
std::vector<double> v3(v2.begin(), v2.end());
for (auto i : v3) {
std::cout << i << " ";
}
std::cout << std::endl;
return 0;
}
```
输出结果为:
```
1 2 3 4 5
1 2 3 4 5
```
其中,第一个vector是从list中初始化的,第二个vector是从vector中初始化的。
相关问题
如何从一个 list<int>初始化一个 vector<double>?从一个 vector<int>又 该如何创建?编写代码验证你的答案。
从一个 `list<int>` 初始化一个 `vector<double>` 可以通过 `vector` 的构造函数来实现,代码如下:
```cpp
#include <iostream>
#include <vector>
#include <list>
using namespace std;
int main() {
list<int> li {1, 2, 3, 4, 5};
vector<double> vd(li.begin(), li.end());
for (double d : vd) {
cout << d << " ";
}
cout << endl;
return 0;
}
```
从一个 `vector<int>` 创建一个 `vector<double>`,也可以通过 `vector` 的构造函数来实现,代码如下:
```cpp
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> vi {1, 2, 3, 4, 5};
vector<double> vd(vi.begin(), vi.end());
for (double d : vd) {
cout << d << " ";
}
cout << endl;
return 0;
}
```
以上两段代码都会输出 `1 2 3 4 5`,即成功将 `list<int>` 和 `vector<int>` 转换成了 `vector<double>`。
如何用c++初始化一个城市信息列表,即动态申请线性表空间
可以使用 C++ STL 中的 vector 容器来动态申请线性表空间,具体代码如下:
```c++
#include <iostream>
#include <vector>
using namespace std;
struct City {
string name; // 城市名称
int population; // 城市人口
double area; // 城市面积
};
int main() {
int n; // 城市数量
cout << "请输入城市数量:" << endl;
cin >> n;
// 动态申请线性表空间
vector<City> city_list(n);
// 初始化城市信息列表
for (int i = 0; i < n; i++) {
cout << "请输入第 " << i + 1 << " 个城市的信息:" << endl;
cout << "城市名称:";
cin >> city_list[i].name;
cout << "城市人口:";
cin >> city_list[i].population;
cout << "城市面积:";
cin >> city_list[i].area;
}
// 打印城市信息列表
cout << "城市信息列表:" << endl;
for (int i = 0; i < n; i++) {
cout << "城市名称:" << city_list[i].name << ",";
cout << "城市人口:" << city_list[i].population << ",";
cout << "城市面积:" << city_list[i].area << endl;
}
return 0;
}
```
上面的代码中,我们首先输入城市数量 n,然后使用 vector 容器动态申请 n 个 City 结构体类型的空间,即 `vector<City> city_list(n)`。接着,使用 for 循环遍历城市信息列表,依次输入每个城市的名称、人口和面积,将其存储到 city_list 数组中。最后,再次使用 for 循环遍历 city_list 数组,将城市信息打印出来。
相关推荐
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)