int getSecond(int a,int b,int c) { return a > b ? (b > c ? b : ( a > c ? c : a)) : ( a > c ? a: (b > c ? c : a)); }
时间: 2024-05-20 15:15:21 浏览: 12
这个函数的作用是返回三个整数中第二小的数。
可以理解为,首先比较 a 和 b,如果 a 大于 b,那么就再比较 b 和 c,返回其中较小的一个。反之,如果 a 小于等于 b,那么就比较 a 和 c,返回其中较小的一个。这样得到的结果就是前两个数中较小的一个和第三个数中较小的一个中较大的一个,即三个数中第二小的数。
不过这个函数的实现有点复杂,可以用一个数组存放这三个数,然后排序后返回第二个数,会比这个实现简单明了。
相关问题
给下面每一行代码给上注释#include "pch.h" #include "FinBudgetFlowManager.h" int FinBudgetFlowManager::C(double _value, time_t _time, CString _remark, FlowType _type) { FinBudgetFlow* pFBD = new FinBudgetFlow(); pFBD->setValue(_value); pFBD->setTime(_time); pFBD->setRemark(_remark); pFBD->setType(_type);; this->FlowMap.insert(std::map<int, FinBudgetFlow*>::value_type(this->getSize(), pFBD)); return this->getSize()-1; } bool FinBudgetFlowManager::U(int _index, double _value, time_t _time, CString _remark, FlowType _type){ std::map<int, FinBudgetFlow*>::iterator iter; iter = this->FlowMap.find(_index); if (iter != this->FlowMap.end()) { delete iter->second; iter->second = NULL; this->FlowMap.erase(iter); FinBudgetFlow *pFBD = new FinBudgetFlow(); pFBD->setValue(_value); pFBD->setTime(_time); pFBD->setRemark(_remark); pFBD->setType(_type); this->FlowMap.insert(std::map<int, FinBudgetFlow*>::value_type(_index, pFBD)); return true; } else { throw _index; } } FinBudgetFlow* FinBudgetFlowManager::R(int _index) { std::map<int, FinBudgetFlow*>::iterator iter; iter = this->FlowMap.find(_index); if (iter != this->FlowMap.end()) { return iter->second; } else { throw _index; } } bool FinBudgetFlowManager::D(int _index) { std::map<int, FinBudgetFlow*>::iterator iter; iter = this->FlowMap.find(_index); if (iter != this->FlowMap.end()) { delete iter->second; iter->second = NULL; //防止野指针这一步是否真的需要? this->FlowMap.erase(iter); return true; } else { throw _index; } } int FinBudgetFlowManager::getSize() { return FlowMap.size(); }
//引入预编译头文件
#include "pch.h"
//引入头文件"FinBudgetFlowManager.h"
#include "FinBudgetFlowManager.h"
//定义函数C,返回类型为int,参数为value,time,remark和type
int FinBudgetFlowManager::C(double _value, time_t _time, CString _remark, FlowType _type) {
//创建一个新的FinBudgetFlow对象
FinBudgetFlow* pFBD = new FinBudgetFlow();
//设置新对象的value
pFBD->setValue(_value);
//设置新对象的time
pFBD->setTime(_time);
//设置新对象的remark
pFBD->setRemark(_remark);
//设置新对象的type
pFBD->setType(_type);;
//将新对象插入到FlowMap中
this->FlowMap.insert(std::map<int, FinBudgetFlow*>::value_type(this->getSize(), pFBD));
//返回FlowMap的大小减一
return this->getSize()-1;
}
//定义函数U,返回类型为bool,参数为index,value,time,remark和type
bool FinBudgetFlowManager::U(int _index, double _value, time_t _time, CString _remark, FlowType _type){
//定义一个迭代器
std::map<int, FinBudgetFlow*>::iterator iter;
//在FlowMap中查找index
iter = this->FlowMap.find(_index);
//如果找到了
if (iter != this->FlowMap.end()) {
//删除原有对象
delete iter->second;
iter->second = NULL;
//从FlowMap中删除原有对象
this->FlowMap.erase(iter);
//创建一个新的FinBudgetFlow对象
FinBudgetFlow *pFBD = new FinBudgetFlow();
//设置新对象的value
pFBD->setValue(_value);
//设置新对象的time
pFBD->setTime(_time);
//设置新对象的remark
pFBD->setRemark(_remark);
//设置新对象的type
pFBD->setType(_type);
//将新对象插入到FlowMap中
this->FlowMap.insert(std::map<int, FinBudgetFlow*>::value_type(_index, pFBD));
//返回true
return true;
}
//如果没有找到
else {
//抛出异常
throw _index;
}
}
//定义函数R,返回类型为FinBudgetFlow*,参数为index
FinBudgetFlow* FinBudgetFlowManager::R(int _index) {
//定义一个迭代器
std::map<int, FinBudgetFlow*>::iterator iter;
//在FlowMap中查找index
iter = this->FlowMap.find(_index);
//如果找到了
if (iter != this->FlowMap.end()) {
//返回找到的对象
return iter->second;
}
//如果没有找到
else {
//抛出异常
throw _index;
}
}
//定义函数D,返回类型为bool,参数为index
bool FinBudgetFlowManager::D(int _index) {
//定义一个迭代器
std::map<int, FinBudgetFlow*>::iterator iter;
//在FlowMap中查找index
iter = this->FlowMap.find(_index);
//如果找到了
if (iter != this->FlowMap.end()) {
//删除对象
delete iter->second;
iter->second = NULL;
//从FlowMap中删除对象
this->FlowMap.erase(iter);
//返回true
return true;
}
//如果没有找到
else {
//抛出异常
throw _index;
}
}
//定义函数getSize,返回类型为int
int FinBudgetFlowManager::getSize() {
//返回FlowMap的大小
return FlowMap.size();
}
std::vector<std::tuple<int32_t, std::string, std::string, std::string>> pageInfo;获取第二个元素
要获取std::vector<std::tuple<int32_t, std::string, std::string, std::string>> pageInfo中的第二个元素,可以使用std::get函数和索引值1来获取。具体代码如下:
```cpp
#include <iostream>
#include <vector>
#include <tuple>
int main() {
std::vector<std::tuple<int32_t, std::string, std::string, std::string>> pageInfo; pageInfo.push_back(std::make_tuple(1, "apple", "red", "fruit"));
pageInfo.push_back(std::make_tuple(2, "banana", "yellow", "fruit"));
pageInfo.push_back(std::make_tuple(3, "orange", "orange", "fruit"));
std::string secondElement = std::get<1>(pageInfo[0]);
std::cout << "Second element: " << secondElement << std::endl;
return 0;
}
```
输出结果为:
```
Second element: apple
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/x-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)