fork download
  1. #include<iostream>
  2. #include<set>
  3.  
  4. /*
  5.  Given a class with two public member variables
  6.  string str and int i and class name
  7.  StringInt, use the STL set to sort a
  8.  vector of StringInt objects by their
  9.  string and then create another STL
  10.  set that sorts them by their integer."
  11.  */
  12.  
  13. class StringInt
  14. {
  15. public:
  16. StringInt(std::string str_in, int i_in) :
  17. str(str_in),
  18. i(i_in)
  19. {}
  20.  
  21. std::string str;
  22. int i;
  23. };
  24.  
  25.  
  26. int main()
  27. {
  28. StringInt a3("a", 3);
  29. StringInt b2("b", 2);
  30. StringInt c1("c", 1);
  31.  
  32. std::set<StringInt, std::less<std::string> > string_sorter;
  33. std::set<StringInt, std::less<int> > int_sorter;
  34.  
  35. string_sorter.emplace(a3);
  36. string_sorter.emplace(b2);
  37. string_sorter.emplace(c1);
  38.  
  39. int_sorter.emplace(a3);
  40. int_sorter.emplace(b2);
  41. int_sorter.emplace(c1);
  42.  
  43. std::cout << string_sorter[0] << std::endl;
  44. std::cout << string_sorter[1] << std::endl;
  45. std::cout << string_sorter[2] << std::endl;
  46. std::cout << int_sorter[0] << std::endl;
  47. std::cout << int_sorter[1] << std::endl;
  48. std::cout << int_sorter[2] << std::endl;
  49. }
Compilation error #stdin compilation error #stdout 0s 15240KB
stdin
12345 54321
compilation info
prog.cpp:43:31: error: type 'std::set<StringInt, std::less<std::string> >' (aka 'set<StringInt, less<basic_string<char> > >') does not provide a subscript operator
    std::cout << string_sorter[0] << std::endl;
                 ~~~~~~~~~~~~~^~
prog.cpp:44:31: error: type 'std::set<StringInt, std::less<std::string> >' (aka 'set<StringInt, less<basic_string<char> > >') does not provide a subscript operator
    std::cout << string_sorter[1] << std::endl;
                 ~~~~~~~~~~~~~^~
prog.cpp:45:31: error: type 'std::set<StringInt, std::less<std::string> >' (aka 'set<StringInt, less<basic_string<char> > >') does not provide a subscript operator
    std::cout << string_sorter[2] << std::endl;
                 ~~~~~~~~~~~~~^~
prog.cpp:46:28: error: type 'std::set<StringInt, std::less<int> >' does not provide a subscript operator
    std::cout << int_sorter[0] << std::endl;
                 ~~~~~~~~~~^~
prog.cpp:47:28: error: type 'std::set<StringInt, std::less<int> >' does not provide a subscript operator
    std::cout << int_sorter[1] << std::endl;
                 ~~~~~~~~~~^~
prog.cpp:48:28: error: type 'std::set<StringInt, std::less<int> >' does not provide a subscript operator
    std::cout << int_sorter[2] << std::endl;
                 ~~~~~~~~~~^~
In file included from prog.cpp:2:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/set:60:
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/bits/stl_tree.h:1836:13: error: no matching function for call to object of type 'std::less<std::__cxx11::basic_string<char> >'
          __comp = _M_impl._M_key_compare(__k, _S_key(__x));
                   ^~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/bits/stl_tree.h:2154:19: note: in instantiation of member function 'std::_Rb_tree<StringInt, StringInt, std::_Identity<StringInt>, std::less<std::__cxx11::basic_string<char> >, std::allocator<StringInt> >::_M_get_insert_unique_pos' requested here
            auto __res = _M_get_insert_unique_pos(_S_key(__z));
                         ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/bits/stl_set.h:434:16: note: in instantiation of function template specialization 'std::_Rb_tree<StringInt, StringInt, std::_Identity<StringInt>, std::less<std::__cxx11::basic_string<char> >, std::allocator<StringInt> >::_M_emplace_unique<StringInt &>' requested here
        { return _M_t._M_emplace_unique(std::forward<_Args>(__args)...); }
                      ^
prog.cpp:35:19: note: in instantiation of function template specialization 'std::set<StringInt, std::less<std::__cxx11::basic_string<char> >, std::allocator<StringInt> >::emplace<StringInt &>' requested here
    string_sorter.emplace(a3);
                  ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/bits/stl_function.h:385:7: note: candidate function not viable: no known conversion from 'const key_type' (aka 'const StringInt') to 'const std::__cxx11::basic_string<char>' for 1st argument
      operator()(const _Tp& __x, const _Tp& __y) const
      ^
In file included from prog.cpp:2:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/set:60:
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/bits/stl_tree.h:1847:11: error: no matching function for call to object of type 'std::less<std::__cxx11::basic_string<char> >'
      if (_M_impl._M_key_compare(_S_key(__j._M_node), __k))
          ^~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/bits/stl_tree.h:2154:19: note: in instantiation of member function 'std::_Rb_tree<StringInt, StringInt, std::_Identity<StringInt>, std::less<std::__cxx11::basic_string<char> >, std::allocator<StringInt> >::_M_get_insert_unique_pos' requested here
            auto __res = _M_get_insert_unique_pos(_S_key(__z));
                         ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/bits/stl_set.h:434:16: note: in instantiation of function template specialization 'std::_Rb_tree<StringInt, StringInt, std::_Identity<StringInt>, std::less<std::__cxx11::basic_string<char> >, std::allocator<StringInt> >::_M_emplace_unique<StringInt &>' requested here
        { return _M_t._M_emplace_unique(std::forward<_Args>(__args)...); }
                      ^
prog.cpp:35:19: note: in instantiation of function template specialization 'std::set<StringInt, std::less<std::__cxx11::basic_string<char> >, std::allocator<StringInt> >::emplace<StringInt &>' requested here
    string_sorter.emplace(a3);
                  ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/bits/stl_function.h:385:7: note: candidate function not viable: no known conversion from 'const StringInt' to 'const std::__cxx11::basic_string<char>' for 1st argument
      operator()(const _Tp& __x, const _Tp& __y) const
      ^
In file included from prog.cpp:2:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/set:60:
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/bits/stl_tree.h:2099:11: error: no matching function for call to object of type 'std::less<std::__cxx11::basic_string<char> >'
                            || _M_impl._M_key_compare(_S_key(__z),
                               ^~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/bits/stl_tree.h:2156:20: note: in instantiation of member function 'std::_Rb_tree<StringInt, StringInt, std::_Identity<StringInt>, std::less<std::__cxx11::basic_string<char> >, std::allocator<StringInt> >::_M_insert_node' requested here
              return _Res(_M_insert_node(__res.first, __res.second, __z), true);
                          ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/bits/stl_set.h:434:16: note: in instantiation of function template specialization 'std::_Rb_tree<StringInt, StringInt, std::_Identity<StringInt>, std::less<std::__cxx11::basic_string<char> >, std::allocator<StringInt> >::_M_emplace_unique<StringInt &>' requested here
        { return _M_t._M_emplace_unique(std::forward<_Args>(__args)...); }
                      ^
prog.cpp:35:19: note: in instantiation of function template specialization 'std::set<StringInt, std::less<std::__cxx11::basic_string<char> >, std::allocator<StringInt> >::emplace<StringInt &>' requested here
    string_sorter.emplace(a3);
                  ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/bits/stl_function.h:385:7: note: candidate function not viable: no known conversion from 'const StringInt' to 'const std::__cxx11::basic_string<char>' for 1st argument
      operator()(const _Tp& __x, const _Tp& __y) const
      ^
In file included from prog.cpp:2:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/set:60:
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/bits/stl_tree.h:1836:13: error: no matching function for call to object of type 'std::less<int>'
          __comp = _M_impl._M_key_compare(__k, _S_key(__x));
                   ^~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/bits/stl_tree.h:2154:19: note: in instantiation of member function 'std::_Rb_tree<StringInt, StringInt, std::_Identity<StringInt>, std::less<int>, std::allocator<StringInt> >::_M_get_insert_unique_pos' requested here
            auto __res = _M_get_insert_unique_pos(_S_key(__z));
                         ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/bits/stl_set.h:434:16: note: in instantiation of function template specialization 'std::_Rb_tree<StringInt, StringInt, std::_Identity<StringInt>, std::less<int>, std::allocator<StringInt> >::_M_emplace_unique<StringInt &>' requested here
        { return _M_t._M_emplace_unique(std::forward<_Args>(__args)...); }
                      ^
prog.cpp:39:16: note: in instantiation of function template specialization 'std::set<StringInt, std::less<int>, std::allocator<StringInt> >::emplace<StringInt &>' requested here
    int_sorter.emplace(a3);
               ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/bits/stl_function.h:385:7: note: candidate function not viable: no known conversion from 'const key_type' (aka 'const StringInt') to 'const int' for 1st argument
      operator()(const _Tp& __x, const _Tp& __y) const
      ^
In file included from prog.cpp:2:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/set:60:
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/bits/stl_tree.h:1847:11: error: no matching function for call to object of type 'std::less<int>'
      if (_M_impl._M_key_compare(_S_key(__j._M_node), __k))
          ^~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/bits/stl_function.h:385:7: note: candidate function not viable: no known conversion from 'const StringInt' to 'const int' for 1st argument
      operator()(const _Tp& __x, const _Tp& __y) const
      ^
In file included from prog.cpp:2:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/set:60:
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/bits/stl_tree.h:2099:11: error: no matching function for call to object of type 'std::less<int>'
                            || _M_impl._M_key_compare(_S_key(__z),
                               ^~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/bits/stl_tree.h:2156:20: note: in instantiation of member function 'std::_Rb_tree<StringInt, StringInt, std::_Identity<StringInt>, std::less<int>, std::allocator<StringInt> >::_M_insert_node' requested here
              return _Res(_M_insert_node(__res.first, __res.second, __z), true);
                          ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/bits/stl_set.h:434:16: note: in instantiation of function template specialization 'std::_Rb_tree<StringInt, StringInt, std::_Identity<StringInt>, std::less<int>, std::allocator<StringInt> >::_M_emplace_unique<StringInt &>' requested here
        { return _M_t._M_emplace_unique(std::forward<_Args>(__args)...); }
                      ^
prog.cpp:39:16: note: in instantiation of function template specialization 'std::set<StringInt, std::less<int>, std::allocator<StringInt> >::emplace<StringInt &>' requested here
    int_sorter.emplace(a3);
               ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/bits/stl_function.h:385:7: note: candidate function not viable: no known conversion from 'const StringInt' to 'const int' for 1st argument
      operator()(const _Tp& __x, const _Tp& __y) const
      ^
12 errors generated.
stdout
Standard output is empty