[Solved] why is cin/cout slower than scanf/ printf

The speed difference is largely due to the iostream I/O functions maintaining synchronization with the C I/O functions. We can turn this off with a call to std::ios::sync_with_stdio(false); By default standard C++ streams are synchronized to the standard C stream after each input/output operation. Once synchronization is turned off, the C++ standard streams are allowed … Read more

[Solved] Vector iterators incompatible error for a vector holding iterators of another vector

push_back might cause a reallocation of the data contained in the vector. And that reallocation will make all iterators to the vector invalid. Dereferencing invalid iterators leads to undefined behavior. Indexes into the vector will continue to stay valid, unless you remove elements from the vector. 2 solved Vector iterators incompatible error for a vector … Read more

[Solved] do I need a virtual function

It appears that what you want is for your Derived class to support the following interface: someInfo si; someMoreInfo smi; Base* pB = new Derived; // Setting info pB->setInfo(si); // Set the `Base::c` member of `*pB` pB->setInfo(smi); // Set the `Derived::g` member of `*pB` // Getting info someInfo pB_si = pB->getInfo(); // Get `Base::c` from … Read more

[Solved] not able to compile code with “std::pair” [closed]

You cannot declare a member variable of a class like that. Declare it as: typedef std::pair <int, int> MyMove; static const MyMove my_no_move; And then define it outside the class as: // The typedef MyMove is also scoped const ConnectFourState::MyMove ConnectFourState::my_no_move(-1, -1); 3 solved not able to compile code with “std::pair” [closed]

[Solved] Transform std::pair to std::tuple with any number of elements

Option #1 #include <cstddef> #include <type_traits> #include <utility> #include <tuple> template <typename A, typename B> struct merge_tuples { static_assert(std::tuple_size<A>::value == std::tuple_size<B>::value, “!”); template <std::size_t… Is> static auto merge(std::index_sequence<Is…>) noexcept -> std::tuple<typename std::decay<decltype(std::declval<typename std::tuple_element<Is, A>::type>() + std::declval<typename std::tuple_element<Is, B>::type>()) >::type…>; using type = decltype(merge(std::make_index_sequence<std::tuple_size<A>::value>{})); }; DEMO Option #2 #include <type_traits> #include <utility> template <typename A, typename … Read more

[Solved] Unsigned int not working C++

You are expecting the cast from int to unsigned int to simply change the sign of a negative value while maintaining its magnitude. But that isn’t how it works in C or C++. when it comes to overflow, unsigned integers follow modular arithmetic, meaning that assigning or initializing from negatives values such as -1 or … Read more

[Solved] I can’t get enum class operators to work

A binary operator member should only take one parameter, the right-hand argument – the left-hand argument is *this. template<int NMAX> typename FastOut<NMAX>::Flags FastOut<NMAX>::operator&(Flags rhs) const { // Return *this & rhs } solved I can’t get enum class operators to work

[Solved] What does this line of code mean

This is not standard C++ program. Zero size arrays are not allowed in C & C++. You should use -pedantic-errors command line option if you are using g++ & clang++ compiler to strictly confirm to the standard & disable any compiler extensions. See live demo here. Clang++ says source_file.cpp:7:14: error: zero size arrays are an … Read more

[Solved] Why sscanf is not handling or ignoring spaces in c++?

Why sscanf is not handling or ignoring spaces in c++? How else should sscanf seperate words/tokens? From its documentation: [“%s”] matches a sequence of non-whitespace characters (a string) I don’t think you can handle this with sscanf or in a one-liner at all. You could do this with std::string::find_first_of and std::string::substr Edit: std::regex_match might be … Read more

[Solved] How can I double a time value in C++ [closed]

Use ONE of the file-handling routines in C++ or C. Mixing FILE and ifstream is certain to cause problems. ifstream input; input.open(“time.in”); input>> hrs; input.get(); … should do the trick. If you want to be picky: if (input.get() != ‘:’) … complain about bad input … 0 solved How can I double a time value … Read more