Even though I inserted 20 (distinct) elements into my std::set, it only has a size of 14?

typedef vector<int> vec; struct classcomp { bool operator() (const vec& vec1, const vec& vec2) const{ if (vec1[0] > vec2[0]){ return true; } return false; } }; int main() { set<vec,classcomp> v; for (int i = 0; i < 20; i++){ v.insert({rand()%20,i}); } cout << "size: " << v.size() << endl; for (auto e : v){… Read More Even though I inserted 20 (distinct) elements into my std::set, it only has a size of 14?

Why are the elements of the std::multimap changing after exiting the loop in this code?

I am trying to implement a transparency in my OpenGL prorgam. Currently I have such function (minimal reproducible example): #include <map> #include <unordered_map> #include <iostream> #include <vector> class Sprite { public: std::vector<int> m_meshes; std::pair<float, float> pos; }; int main() { std::unordered_map<int, Sprite> m_sprites = { { 0, { { 43 } } }, { 1,… Read More Why are the elements of the std::multimap changing after exiting the loop in this code?

No warning message generated by g++ when using [[nodiscard]] and compile with lower stl option

is there nodiscard attribute available in c++11? If not, why no warning message output by g++ code belows. //test.cpp #include<iostream> [[nodiscard]] int foo() { return 1; } int main() { std::cout<<foo()<<std::endl; return 0; } At the beginning, i complie the program by "g++ -std=c++20 test.cpp -o test.o" and everythings is fine. But then i want… Read More No warning message generated by g++ when using [[nodiscard]] and compile with lower stl option

How to apply function that returns Result to each element of HashSet in Rust

As a language that aims for safety and performance, Rust provides a powerful data structure called HashSet that provides a fast and efficient way to store and retrieve unique values. HashSet is optimized for scenarios where you need to check for the presence of a value and avoid duplicates, making it a popular choice among… Read More How to apply function that returns Result to each element of HashSet in Rust

why can I use a class as custom comparison criterion as a template argument to std::map without providing a class instance

I’m reading the std::map part of The C++ Standard Library by Nicolai M. Josuttis. Here is a simplified code which can show the question bother me a lot: #include <iostream> #include <map> #include <string> class MyCompare { public: bool operator() (const std::string& lhs, const std::string& rhs) const { return lhs < rhs; } }; int… Read More why can I use a class as custom comparison criterion as a template argument to std::map without providing a class instance

Return the elements in an array using operator overloading with a class passing through it

I want to create a dynamic memory for a car dealership for bikes and cars but the vector didn’t print the right things and has lots of repetitions that aren’t supposed to be there. I created an object for the overlapping variables for both vehicles so I can pass it through the bike class later… Read More Return the elements in an array using operator overloading with a class passing through it