c++ 为什么gquiz1的上限值小于键值

m3eecexj  于 2022-12-01  发布在  其他
关注(0)|答案(1)|浏览(156)
  1. // CPP Program to demonstrate the
  2. // implementation of multiset
  3. #include <iostream>
  4. #include <iterator>
  5. #include <set>
  6. using namespace std;
  7. int main()
  8. {
  9. // empty multiset container
  10. multiset<int, greater<int> > gquiz1;
  11. // insert elements in random order
  12. gquiz1.insert(40);
  13. gquiz1.insert(30);
  14. gquiz1.insert(60);
  15. gquiz1.insert(20);
  16. gquiz1.insert(50);
  17. // 50 will be added again to
  18. // the multiset unlike set
  19. gquiz1.insert(50);
  20. gquiz1.insert(10);
  21. // printing multiset gquiz1
  22. multiset<int, greater<int> >::iterator itr;
  23. cout << "\nThe multiset gquiz1 is : \n";
  24. for (itr = gquiz1.begin(); itr != gquiz1.end(); ++itr) {
  25. cout << *itr << " ";
  26. }
  27. cout << endl;
  28. // assigning the elements from gquiz1 to gquiz2
  29. multiset<int> gquiz2(gquiz1.begin(), gquiz1.end());
  30. // print all elements of the multiset gquiz2
  31. cout << "\nThe multiset gquiz2 \n"
  32. "after assign from gquiz1 is : \n";
  33. for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) {
  34. cout << *itr << " ";
  35. }
  36. cout << endl;
  37. // remove all elements up to element
  38. // with value 30 in gquiz2
  39. cout << "\ngquiz2 after removal \n"
  40. "of elements less than 30 : \n";
  41. gquiz2.erase(gquiz2.begin(), gquiz2.find(30));
  42. for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) {
  43. cout << *itr << " ";
  44. }
  45. // remove all elements with value 50 in gquiz2
  46. int num;
  47. num = gquiz2.erase(50);
  48. cout << "\ngquiz2.erase(50) : \n";
  49. cout << num << " removed \n";
  50. for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) {
  51. cout << *itr << " ";
  52. }
  53. cout << endl;
  54. // lower bound and upper bound for multiset gquiz1
  55. cout << "\ngquiz1.lower_bound(40) : \n"
  56. << *gquiz1.lower_bound(40) << endl;
  57. cout << "gquiz1.upper_bound(40) : \n"
  58. << *gquiz1.upper_bound(40) << endl;
  59. // lower bound and upper bound for multiset gquiz2
  60. cout << "gquiz2.lower_bound(40) : \n"
  61. << *gquiz2.lower_bound(40) << endl;
  62. cout << "gquiz2.upper_bound(40) : \n"
  63. << *gquiz2.upper_bound(40) << endl;
  64. return 0;
  65. }

gquiz1的上限打印30,关键字为40。gquiz2的上限似乎只给出了一个更高的值。我认为这将与多集gquiz1中的多个类似元素有关,但在所有擦除函数后,在gquiz2中插入50两次,gquiz2的上限仍然给出了比关键字更高的值,而gquiz1给出了更低的值。请帮助。
修改了两次插入50的代码。我尝试了两次插入50的代码:

  1. // CPP Program to demonstrate the
  2. // implementation of multiset
  3. #include <iostream>
  4. #include <iterator>
  5. #include <set>
  6. using namespace std;
  7. int main()
  8. {
  9. // empty multiset container
  10. multiset<int, greater<int> > gquiz1;
  11. // insert elements in random order
  12. gquiz1.insert(40);
  13. gquiz1.insert(30);
  14. gquiz1.insert(60);
  15. gquiz1.insert(20);
  16. gquiz1.insert(50);
  17. // 50 will be added again to
  18. // the multiset unlike set
  19. gquiz1.insert(50);
  20. gquiz1.insert(10);
  21. // printing multiset gquiz1
  22. multiset<int, greater<int> >::iterator itr;
  23. cout << "\nThe multiset gquiz1 is : \n";
  24. for (itr = gquiz1.begin(); itr != gquiz1.end(); ++itr) {
  25. cout << *itr << " ";
  26. }
  27. cout << endl;
  28. // assigning the elements from gquiz1 to gquiz2
  29. multiset<int> gquiz2(gquiz1.begin(), gquiz1.end());
  30. // print all elements of the multiset gquiz2
  31. cout << "\nThe multiset gquiz2 \n"
  32. "after assign from gquiz1 is : \n";
  33. for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) {
  34. cout << *itr << " ";
  35. }
  36. cout << endl;
  37. // remove all elements up to element
  38. // with value 30 in gquiz2
  39. cout << "\ngquiz2 after removal \n"
  40. "of elements less than 30 : \n";
  41. gquiz2.erase(gquiz2.begin(), gquiz2.find(30));
  42. for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) {
  43. cout << *itr << " ";
  44. }
  45. // remove all elements with value 50 in gquiz2
  46. int num;
  47. num = gquiz2.erase(50);
  48. cout << "\ngquiz2.erase(50) : \n";
  49. cout << num << " removed \n";
  50. for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) {
  51. cout << *itr << " ";
  52. }
  53. gquiz2.insert(50);
  54. gquiz2.insert(50);
  55. cout << endl;
  56. // lower bound and upper bound for multiset gquiz1
  57. cout << "\ngquiz1.lower_bound(40) : \n"
  58. << *gquiz1.lower_bound(40) << endl;
  59. cout << "gquiz1.upper_bound(40) : \n"
  60. << *gquiz1.upper_bound(40) << endl;
  61. // lower bound and upper bound for multiset gquiz2
  62. cout << "gquiz2.lower_bound(40) : \n"
  63. << *gquiz2.lower_bound(40) << endl;
  64. cout << "gquiz2.upper_bound(40) : \n"
  65. << *gquiz2.upper_bound(40) << endl;
  66. return 0;
  67. }

除外:

  1. The multiset gquiz1 is :
  2. 60 50 50 40 30 20 10
  3. The multiset gquiz2
  4. after assign from gquiz1 is :
  5. 10 20 30 40 50 50 60
  6. gquiz2 after removal
  7. of elements less than 30 :
  8. 30 40 50 50 60
  9. gquiz2.erase(50) :
  10. 2 removed
  11. 30 40 60
  12. gquiz1.lower_bound(40) :
  13. 40
  14. gquiz1.upper_bound(40) :
  15. 30
  16. gquiz2.lower_bound(40) :
  17. 40
  18. gquiz2.upper_bound(40) :
  19. 30

实际值:

  1. The multiset gquiz1 is :
  2. 60 50 50 40 30 20 10
  3. The multiset gquiz2
  4. after assign from gquiz1 is :
  5. 10 20 30 40 50 50 60
  6. gquiz2 after removal
  7. of elements less than 30 :
  8. 30 40 50 50 60
  9. gquiz2.erase(50) :
  10. 2 removed
  11. 30 40 60
  12. gquiz1.lower_bound(40) :
  13. 40
  14. gquiz1.upper_bound(40) :
  15. 30
  16. gquiz2.lower_bound(40) :
  17. 40
  18. gquiz2.upper_bound(40) :
  19. 50
h7wcgrx3

h7wcgrx31#

你的问题就在这里

  1. multiset<int, greater<int> > gquiz1;

因为您使用的是greater<int>,所以upper_boundlower_bound的通常意义已经改变。使用upper_bound将返回值 * 小于 * 键的第一个元素,而lower_bound将返回值 * 大于或等于 * 键的第一个元素。
upper_bound的精确定义是,对于map〈T,C〉和给定的key,它返回第一个C(key,x)为真的元素x。如果C是大于运算,那么这个定义意味着你将得到第一个小于key的元素。换句话说,upper_bound的通常含义是相反的。
如果更改为此

  1. multiset<int> gquiz1;

您将看到upper_boundlower_bound的通常含义返回。

相关问题