std::vector<int> arr(5, 100); for(auto &i : arr) { std::cout << i << std::endl; } 这段代码会输出啥
时间: 2024-04-22 08:21:53 浏览: 13
这段代码会输出五个数字 100,每个数字一行。原因是,这段代码中创建了一个包含五个元素的 `std::vector<int>` 数组 `arr`,并将每个元素初始化为 100。然后,使用 for-each 循环遍历 `arr` 中的每个元素,将其依次赋值给变量 `i`,并输出到标准输出流中。由于 `arr` 中每个元素都是 100,因此输出的结果也都是 100。
相关问题
下面这段代码的错误是什么#include <iostream> #include <vector> namespace my_std { using std::cin; using std::cout; using std::endl; using std::swap; using std::vector; } using namespace my_std; class MySort { public: /选择排序/ template<typename T> void SelectionSort(vector<T> &arr); }; void (SelectionPtr) (vector<int>) = &MySort::SelectionSort<vector<int>>; //指针函数
这段代码存在一个类型不匹配的错误。具体来说,`SelectionPtr` 的定义中,成员函数 `MySort::SelectionSort` 接受的参数类型为 `vector<T>&`,而指针变量 `SelectionPtr` 的类型为 `void (*)(vector<int>)`,两者的参数类型并不匹配。
为了解决这个错误,需要将 `SelectionPtr` 的类型定义修改为能够匹配 `MySort::SelectionSort` 函数的类型。具体来说,可以将 `SelectionPtr` 的类型定义为:
```
using SelectionPtr = void (MySort::*)(vector<int>&);
```
这样,`SelectionPtr` 就成为了一个指向 `MySort::SelectionSort` 函数的指针类型,该函数接受一个 `vector<int>&` 类型的引用作为参数,返回值为 `void`。然后,将 `SelectionPtr` 指向 `MySort::SelectionSort<vector<int>>` 的实例化版本即可:
```
SelectionPtr ptr = &MySort::SelectionSort<int>; // 指向 MySort::SelectionSort<vector<int>> 的指针
```
这样就可以消除类型不匹配的错误了。
如何取出std::multimap<int, std::vector<std::array<double, 3> > > ret内的内容
可以使用迭代器来遍历`std::multimap<int, std::vector<std::array<double, 3> > >`类型的变量`ret`,然后将其中的元素取出来。以下是一个示例代码:
```c++
#include <iostream>
#include <map>
#include <vector>
#include <array>
int main()
{
std::multimap<int, std::vector<std::array<double, 3>>> ret = {
{1, {{1.0, 2.0, 3.0}, {4.0, 5.0, 6.0}}},
{2, {{7.0, 8.0, 9.0}, {10.0, 11.0, 12.0}}}
};
// 遍历 multimap
for(auto it = ret.begin(); it != ret.end(); ++it) {
std::cout << "key: " << it->first << std::endl;
// 遍历 vector
for(auto& vec : it->second) {
// 遍历 array
for(auto& arr : vec) {
std::cout << arr[0] << ", " << arr[1] << ", " << arr[2] << std::endl;
}
}
}
return 0;
}
```
输出结果为:
```
key: 1
1, 2, 3
4, 5, 6
key: 2
7, 8, 9
10, 11, 12
```
可以看到,在遍历`std::multimap<int, std::vector<std::array<double, 3> > >`类型的变量时,我们需要先遍历`multimap`的键值对,然后遍历对应的`vector`,最后遍历`vector`中每个`array`的元素。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)