我正在尝试按对中的第一个值对对向量进行排序。我已经尝试按照已经发布的关于此问题的其他问题的答案的建议,但由于某种原因我无法对矢量进行排序。我尝试过使用std :: sort和std :: stable_sort无济于事。代码编译良好,运行没有任何投诉,但数组没有排序。我的示例代码如下:
#include <iostream>
#include <vector>
#include <algorithm>
bool compare(const std::pair<int, int>&i, const std::pair<int, int>&j){
return i.first < j.first;
}
int main(){
std::vector<std::pair<int, char>> vec;
vec.reserve(10); // reserve space for 10 elements
int i;
std::string letters = "abcdefghij";
int randNum;
for(i=0; i<10; i++){
randNum = std::rand()%(10-0 + 1); // generate random numbers between 0 and 10
vec[i].first = randNum; // assign random integer to first element of pair
vec[i].second = letters[i]; // assign letter to second element of pair
}
for(i=0; i<10; i++){ // print out unsorted array
std::cout << vec[i].first << " " << vec[i].second << "\n";
}
std::cout << "\n";
std::sort(vec.begin(), vec.end(), compare);
for(i=0; i<10; i++){ // print out sorted array
std::cout << vec[i].first << " " << vec[i].second << "\n";
}
return 1;
}
结果输出如下:
10 a
1 b
0 c
6 d
8 e
3 f
2 g
0 h
9 i
4 j
10 a
1 b
0 c
6 d
8 e
3 f
2 g
0 h
9 i
4 j
答案 0 :(得分:2)
你实际上没有调整矢量大小,你的矢量是空的。
而不是:
vec.reserve(10);
使用:
vec.resize(10);
如果您使用了基于范围的for循环,这将是显而易见的:
// This won't print anything out at all.
for (const auto &it : vec) {
std::cout << it.first << " " << it.second << "\n";
}
答案 1 :(得分:1)
您可以使用索引到push_back替换初始化向量的for循环。
public class Number0to9 {
public static String input;
public static void main(String[] args) {
tryagain: while (true) {
System.out.print("Type a number between 0 and 9 : ");
Scanner scan = new Scanner(System.in);
int x = scan.nextInt();
int output = x;
input = "You entered number: ";
switch (x) {
case 0:
input += output;
break tryagain;
case 1:
input += output;
break tryagain;
case 2:
input += output;
break tryagain;
case 3:
input += output;
break tryagain;
case 4:
input += output;
break tryagain;
case 5:
input += output;
break tryagain;
case 6:
input += output;
break tryagain;
case 7:
input += output;
break tryagain;
case 8:
input += output;
break tryagain;
case 9:
input += output;
break tryagain;
default:
System.out.println("Your number was bigger than 9");
break;
}
}
}
}
答案 2 :(得分:1)
如果您开始学习C ++,请学习C ++ 11的功能。它将使您的代码更容易编写,并减少错误。这是编写程序,IMO的更好方法,它可以解决您遇到的错误。另外,请注意,main表示返回0表示“ok”,1表示“not ok”
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<std::pair<int, char>> vec;
std::string letters = "abcdefghij";
for(auto l: letters) {
int randNum = std::rand()%(10-0 + 1); // generate random numbers between 0 and 10
vec.push_back({randNum, l}); // use constructor to create the pair
// pushback figures it is a pair that must be
// inserted and calls corresponding constructor (pair
// in this case)
}
for(auto p: vec) {
std::cout << p.first << " " << p.second << "\n";
}
std::cout << "\n";
std::sort(vec.begin(), vec.end(), [](auto a, auto b) { // use lambda. Cleaner and easier to read
return a.first < b.first;
});
for(auto p: vec) {
std::cout << p.first << " " << p.second << "\n";
}
return 0;
}