例如,我有一個矢量為v=[7,4,5,10,13]。這里我的目標數字是6。 我首先對該向量進行排序
sort(v.begin(), v.end() 。)
現在我的輸出將是[4,5,7,10,13]。之后,我想列印大于或等于'6'的數字,這將使我的輸出為[7,10,13]。
請建議一個合適的方法在cpp中執行這個任務。
uj5u.com熱心網友回復:
#include <algorithm>/span>
#include <vector>
#include <iostream>
int main()
{
std:: vector<int> v = {7,4,5,10,13}。
std::sort(v.begin(), v.end()。
auto iter = std::upper_bound(v. begin(), v.end(), 6)。)
for (; iter != v.end(); iter)
std::cout << *iter << " "/span>;
}
輸出:
7 10 13
uj5u.com熱心網友回復:
這是我的快速解決方案
std:: vector<int> v = {7,4,5,10,13};
std::sort(v.begin(), v.end()。
std::copy_if(v.begin(), v. end(), std::ostream_iterator<int>(std::cout, " ")。
[](int a){ return a > 6; })
uj5u.com熱心網友回復:
或者,如果你的編譯器已經支持它,請使用
auto larger_then_6 = [](int i) { return i > 6; };
int main()
{
std:: vector<int> v = { 7,4,5,10,13 };
std::sort(v.begin(), v.end()。
for (auto value : v | std::views::filter(bigger_then_6))
{
std::cout << value << " "/span>;
}
std::cout << std::endl;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/321879.html
標籤:
上一篇:使用特定的shopify商店資料在laravel中呼叫Shopifyapi?
下一篇:Css內邊界半徑
