C++ 是否有另一种访问成员的方法,而不是使用 std::vector <std::pair <int, int>> 的范围 for 循环?
C++ is there another way to access members first and second than using a range for loop for std::vector <std::pair <int, int>>?
前几天我解决了另一个问题,涉及一个名为 name
的 std::vector <std::pair<int,int>>
。
我的问题是,如何访问这种类型的name.first
和name.second
?
我最终使用了范围循环,它解决了我的问题
for(i : name) { i->first , i->second}
但是,还有别的办法吗?我对如何在正常的 for
循环中访问它特别感兴趣,例如
for(int i = 0; i < name.size(); i++) { std::vector::std::pair::name.first}
任何人都可以帮我解释一下吗?
通常的方式
for (size_t i = 0; i < name.size(); i++)
{
cout << name[i].first;
cout << name[i].second;
}
这只是访问结构向量(或数组)的典型方法。
顺便说一句,你说的代码实际上并没有,for(i : name) { i->first , i->second}
应该是 for(i : name) { i.first , i.second}
。您的版本适用于指针对向量,但不适用于指针对向量。
这是一个关于 C++ 语法的非常基础的问题。我推荐一本书而不是 Stack Overflow。
执行此操作的优雅方法是:
std::vector<std::pair<int, int>> names;
for(int i = 0; i < names.size(); i++) {
auto &name = names[i];
// now access name.first, name.second
}
在 C++17 中,您可以使用 structured binding
for (auto & [a, b] : name) {
// a is a reference to the first of each pair
// b is a reference to the second of each pair
}
如果您使用的是 C++17(或更高版本),结构绑定可以做到这一点
#include <vector>
#include <utility>
int main()
{
std::vector <std::pair<int,int>> vp;
for (auto & [first,second] : vp)
{
// do something with first and second
};
}
见https://en.cppreference.com/w/cpp/language/structured_binding
从 C++17 开始,您可以使用 structured bindings:
#include <iostream>
#include <vector>
#include <tuple>
int main() {
std::vector<std::pair<int,int>> v{};
v.push_back({1, 11});
v.push_back({2, 22});
for (auto [a, b] : v) {
std::cout << a << " " << b << "\n";
} // 1 11
// 2 22
}
请注意,结构化绑定声明中的 auto
表示每对都按值获取(这在处理基本类型时是合理的)。
如果您想读取非基本类型,或通过结构化绑定标识符写入,您可以分别使用 auto const&
或 auto&
。例如:
// add 'first' to 'second;
for (auto& [a, b] : v) {
b += a;
} // 1 11
// 2 22
// read only (by value)
for (auto [a, b] : v) {
std::cout << a << " " << b << "\n";
} // 1 12
// 2 24
#include <iostream>
#include <vector>
#include <utility>
int main()
{
std::vector<std::pair<int, int>> somePairs{ {1, 2}, {5, 10}, {12, 60} };
for (auto [first, second] : somePairs)
{
std::cout << "First = " << first << ", second = " << second << '\n';
}
return 0;
}
这会自动将 std::vector
中的 std::pair
解包到 first
和 second
变量中。
输出:
First = 1, second = 2
First = 5, second = 10
First = 12, second = 60
unordered_map<int , int> mp;
for(auto x:mp)
{
cout<<x.first;
cout<<x.second;
}
这是您在标准模板库 (STL) 中访问成员 first 和 second 的方式,方法是创建一个具有键值对的容器和一个具有唯一对的映射值。
前几天我解决了另一个问题,涉及一个名为 name
的 std::vector <std::pair<int,int>>
。
我的问题是,如何访问这种类型的name.first
和name.second
?
我最终使用了范围循环,它解决了我的问题
for(i : name) { i->first , i->second}
但是,还有别的办法吗?我对如何在正常的 for
循环中访问它特别感兴趣,例如
for(int i = 0; i < name.size(); i++) { std::vector::std::pair::name.first}
任何人都可以帮我解释一下吗?
通常的方式
for (size_t i = 0; i < name.size(); i++)
{
cout << name[i].first;
cout << name[i].second;
}
这只是访问结构向量(或数组)的典型方法。
顺便说一句,你说的代码实际上并没有,for(i : name) { i->first , i->second}
应该是 for(i : name) { i.first , i.second}
。您的版本适用于指针对向量,但不适用于指针对向量。
这是一个关于 C++ 语法的非常基础的问题。我推荐一本书而不是 Stack Overflow。
执行此操作的优雅方法是:
std::vector<std::pair<int, int>> names;
for(int i = 0; i < names.size(); i++) {
auto &name = names[i];
// now access name.first, name.second
}
在 C++17 中,您可以使用 structured binding
for (auto & [a, b] : name) {
// a is a reference to the first of each pair
// b is a reference to the second of each pair
}
如果您使用的是 C++17(或更高版本),结构绑定可以做到这一点
#include <vector>
#include <utility>
int main()
{
std::vector <std::pair<int,int>> vp;
for (auto & [first,second] : vp)
{
// do something with first and second
};
}
见https://en.cppreference.com/w/cpp/language/structured_binding
从 C++17 开始,您可以使用 structured bindings:
#include <iostream>
#include <vector>
#include <tuple>
int main() {
std::vector<std::pair<int,int>> v{};
v.push_back({1, 11});
v.push_back({2, 22});
for (auto [a, b] : v) {
std::cout << a << " " << b << "\n";
} // 1 11
// 2 22
}
请注意,结构化绑定声明中的 auto
表示每对都按值获取(这在处理基本类型时是合理的)。
如果您想读取非基本类型,或通过结构化绑定标识符写入,您可以分别使用 auto const&
或 auto&
。例如:
// add 'first' to 'second;
for (auto& [a, b] : v) {
b += a;
} // 1 11
// 2 22
// read only (by value)
for (auto [a, b] : v) {
std::cout << a << " " << b << "\n";
} // 1 12
// 2 24
#include <iostream>
#include <vector>
#include <utility>
int main()
{
std::vector<std::pair<int, int>> somePairs{ {1, 2}, {5, 10}, {12, 60} };
for (auto [first, second] : somePairs)
{
std::cout << "First = " << first << ", second = " << second << '\n';
}
return 0;
}
这会自动将 std::vector
中的 std::pair
解包到 first
和 second
变量中。
输出:
First = 1, second = 2
First = 5, second = 10
First = 12, second = 60
unordered_map<int , int> mp;
for(auto x:mp)
{
cout<<x.first;
cout<<x.second;
}
这是您在标准模板库 (STL) 中访问成员 first 和 second 的方式,方法是创建一个具有键值对的容器和一个具有唯一对的映射值。