通常,我建议您按照其他答案中的建议使用 std 库排序、partial_sort、nth_element 等。
不过,求一个min并不难,而且避免排序,避免重复向量,
与
如果你在要求时真的是认真的:
我需要一个最简单的 for 循环或 while 语句来找到 10
没有排序的向量中的最小值?
这是一种方法:(请参阅下面的评论“找到 10 个最小值”)
#include <iomanip>
#include <iostream>
#include <vector>
#include <chrono>
#include <algorithm> // shuffle
typedef std::vector< int > IntVec;
typedef std::vector< size_t > IndxVec;
const size_t SMALLEST_10 = 10;
void myVecShow(IntVec& intVec, const std::string label); // below
// ///////////////////////////////////////////////////////
size_t findIndxOfMinValue(const IntVec& intVec)
{
size_t minIndx = 0; // search from beginning
for (size_t j = 0;
j < intVec.size(); // compare each
++j)
{
if(intVec[j] < intVec[minIndx])
minIndx = j; // capture smallest
}
return(minIndx);
}
// ///////////////////////////////////////////////////////
int t258()
{
IntVec myVec;
{
// fill myVec
for (int i=0; i<100; ++i)
myVec.push_back(100-i); // "my vector for instance has 100 elements in it."
//myVecShow(myVec, "\n unshuffled values: ");
// shuffle myVec
time_t seed = std::chrono::system_clock::now().time_since_epoch().count();
std::shuffle (myVec.begin(), myVec.end(), std::default_random_engine(seed));
myVecShow(myVec, "\n Values: ");
}
IndxVec indxVec; // to be filled with index of the smallest elements
indxVec.reserve(SMALLEST_10);
IntVec origVal; // capture original values of smallest elements
origVal.reserve(SMALLEST_10);
// ///////////////////////////////////////////////////////
// find 10 smallest values
{
for (size_t i=0; i<SMALLEST_10; ++i)
{
size_t minValIndx = findIndxOfMinValue(myVec);
indxVec.push_back (minValIndx); // capture index
origVal.push_back (myVec[minValIndx]); // capture value at index
myVec[minValIndx] = std::numeric_limits<int>::max(); // mark min value (so its no longer min)
}
std::cout << std::endl;
}
// restore original values prior to report
for (size_t i=0; i<indxVec.size(); ++i)
{
size_t indx = indxVec[i];
myVec [indx] = origVal[i];
}
// ///////////////////////////////////////////////////////
// report results
std::cout << "\nSmallest 10 elements: " << std::endl;
std::cout << " indx value" << std::endl;
for (size_t i=0; i<indxVec.size(); ++i)
{
size_t indx = indxVec[i];
std::cout << "myVec[" << std::setw(2) << indx
<< "] = " << std::setw(3) << myVec[indx]
<< std::endl;;
}
std::cout << std::endl;
return(0);
} // int t258(void)
// ///////////////////////////////////////////////////////
void myVecShow(IntVec& intVec, const std::string label)
{
std::cout << label << std::endl;
size_t j = intVec.size() - 1;
// header:
{
std::cout << " ";
for (size_t i=0; i<SMALLEST_10; ++i) std::cout << std::setw(3) << i << " ";
std::cout << std::endl;
}
std::cout << std::setw(3) << 0 << ": ";
for (size_t i=0; i<intVec.size(); ++i)
{
std::cout << std::setw(3) << intVec[i] << " ";
if ((i < j) && (9 == (i % 10)))
{
size_t row = 1 + (i / 10);
std::cout << std::endl << std::setw(3) << row << ": ";
}
}
std::cout << std::endl;
}
结果:(随机洗牌)
Values:
0 1 2 3 4 5 6 7 8 9
0: 50 85 6 62 48 34 40 73 86 11
1: 94 58 46 96 66 56 42 15 25 13
2: 92 30 7 35 65 37 5 69 90 68
3: 100 3 2 87 21 93 43 99 10 98
4: 44 24 70 41 59 95 72 49 78 81
5: 4 23 47 51 36 54 12 67 91 14
6: 53 97 71 52 77 27 20 29 76 83
7: 80 28 17 38 32 16 39 9 74 18
8: 84 31 61 45 8 33 82 55 63 89
9: 60 26 88 79 1 19 57 75 64 22
Smallest 10 elements:
indx value
myVec[94] = 1
myVec[32] = 2
myVec[31] = 3
myVec[50] = 4
myVec[26] = 5
myVec[ 2] = 6
myVec[22] = 7
myVec[84] = 8
myVec[77] = 9
myVec[38] = 10
更新 - 当最小的 10 中的值重复时会发生什么:
我加了
myVec[6] = 7;
重新运行代码(所以,这是一个不同的随机洗牌),输出是:
Smallest 10 elements:
indx value
myVec[ 7] = 0
myVec[78] = 1
myVec[83] = 2
myVec[79] = 3
myVec[11] = 4
myVec[87] = 5
myVec[12] = 7
myVec[67] = 7
myVec[42] = 8
myVec[51] = 9
更新 - 我们如何修改此代码以显示最小 10 个唯一值的索引
注意:未经运行测试,并且此第一次尝试代码(可能)无法正确处理具有少于 10 个唯一值的 100 元素数组
// find 10 smallest **unique** values
{
for (size_t i=0; i<SMALLEST_10; ++i)
{
size_t minValIndx = findIndxOfMinValue(myVec);
indxVec.push_back (minValIndx); // capture index
origVal.push_back (myVec[minValIndx]);
myVec[minValIndx] = std::numeric_limits<int>::max();
// now hide any duplicate of this minValue,
for (size_t j=0; j<myVec.size(); ++j) // search all
{
if(myVec[j] == origVal.back())
{
indxVec.push_back (j); // capture index
origVal.push_back (myVec[j]);
myVec[minValIndx] = std::numeric_limits<int>::max();
}
}
}
std::cout << std::endl;
}