当前位置: 首页> 财经> 创投人物 > 网站推广方法有_免费公司取名_链接买卖价格_营销策划咨询机构

网站推广方法有_免费公司取名_链接买卖价格_营销策划咨询机构

时间:2025/7/12 14:33:49来源:https://blog.csdn.net/m0_74811974/article/details/142397640 浏览次数:0次
网站推广方法有_免费公司取名_链接买卖价格_营销策划咨询机构

C++中的pair是一个模板类,用来存储两个不同类型的对象。它位于<utility>头文件中,并定义在std命名空间中。

pair的定义如下:

template <class T1, class T2>
struct pair {using first_type = T1;using second_type = T2;T1 first;T2 second;// 构造函数constexpr pair();constexpr pair(const T1& x, const T2& y);template<class U1, class U2>constexpr pair(U1&& x, U2&& y);template<class... Args1, class... Args2>constexpr pair(piecewise_construct_t, tuple<Args1...> first_args, tuple<Args2...> second_args);// 拷贝和移动构造函数template<class U1, class U2>constexpr pair(const pair<U1, U2>& other);template<class U1, class U2>constexpr pair(pair<U1, U2>&& other);// 赋值操作符template<class U1, class U2>constexpr pair& operator=(const pair<U1, U2>& other);template<class U1, class U2>constexpr pair& operator=(pair<U1, U2>&& other);constexpr void swap(pair& other) noexcept(noexcept(swap(first, other.first)) && noexcept(swap(second, other.second)));};

pair提供了以下主要功能:

  • firstsecondpair的两个成员变量,用来存储两个不同类型的值。
  • pair的构造函数可以接受两个参数,并根据参数的类型自动推导为firstsecond进行初始化。
  • pair提供了拷贝和移动构造函数,以及赋值操作符,可以方便地进行对象的拷贝、移动和赋值操作。
  • pair的成员变量可以使用std::get函数或者std::tie函数进行访问和解包。
  • pair还提供了swap函数,用来交换两个pair对象的值。

以下是pair的示例用法:

#include <iostream>
#include <utility>int main() {std::pair<int, std::string> p1(1, "one");std::pair<int, std::string> p2 = std::make_pair(2, "two");std::cout << p1.first << " " << p1.second << std::endl;std::cout << p2.first << " " << p2.second << std::endl;p1.second = "updated";std::cout << p1.first << " " << p1.second << std::endl;std::swap(p1, p2);std::cout << p1.first << " " << p1.second << std::endl;std::cout << p2.first << " " << p2.second << std::endl;return 0;
}

输出结果:

1 one
2 two
1 updated
2 updated
1 two

以上示例展示了创建pair对象、访问成员变量、修改成员变量以及交换对象值的操作。

希望以上解释对你有所帮助!如有其他问题,请随时提问。

关键字:网站推广方法有_免费公司取名_链接买卖价格_营销策划咨询机构

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: