c++返回一个pair类型

前言

Under the new standard we can list initialize the return value.

代码测试

cpp 复制代码
#include<iostream>
#include<string>
#include<vector>

std::pair<std::string, int>
process(std::vector<std::string>& v)
{
	if (!v.empty())
	{
		return { v.back(), v.back().size() }; // list initialize返回
	}
	else
	{
		return std::pair<std::string, int>();
	}
}

int main()
{
	std::vector<std::string> vecT{ "hablee","yuki" };
	std::pair<std::string, int> re = process(vecT);
	std::cout << re.first << ", " << re.second << std::endl;

	return 0;
}
相关推荐
会员源码网1 小时前
构造函数抛出异常:C++对象部分初始化的陷阱与应对策略
c++
xlp666hub3 小时前
Leetcode第二题:用 C++ 解决字母异位词分组
c++·leetcode
不想写代码的星星4 小时前
static 关键字:从 C 到 C++,一篇文章彻底搞懂它的“七十二变”
c++
xlp666hub20 小时前
Leetcode第一题:用C++解决两数之和问题
c++·leetcode
不想写代码的星星1 天前
C++继承、组合、聚合:选错了是屎山,选对了是神器
c++
不想写代码的星星2 天前
std::function 详解:用法、原理与现代 C++ 最佳实践
c++
樱木Plus4 天前
深拷贝(Deep Copy)和浅拷贝(Shallow Copy)
c++
blasit6 天前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
肆忆_7 天前
# 用 5 个问题学懂 C++ 虚函数(入门级)
c++
不想写代码的星星7 天前
虚函数表:C++ 多态背后的那个男人
c++