std

Template Template C++ Function

五迷三道 提交于 2019-12-18 08:46:32
问题 How do I write a template function that operates on a arbitrary container of a arbitrary type? For example how do I generalize this dummy function template <typename Element> void print_size(const std::vector<Element> & a) { cout << a.size() << endl; } to template <template<typename> class Container, typename Element> void print_size(const Container<Element> & a) { cout << a.size() << endl; } Here is a typical usage std::vector<std::string> f; print_size(f) This give error tests/t_distances

Is there a standard C++ function object for taking apart a std::pair?

谁都会走 提交于 2019-12-18 07:47:06
问题 Does anyone know if there's a de-facto standard (i.e., TR1 or Boost) C++ function object for accessing the elements of a std::pair? Twice in the past 24 hours I've wished I had something like the keys function for Perl hashes. For example, it would be nice to run std::transform on a std::map object and dump all the keys (or values) to another container. I could certainly write such a function object but I'd prefer to reuse something that's had a lot of eyeballs on it. 回答1: boost::bind is what

Is there a standard C++ function object for taking apart a std::pair?

坚强是说给别人听的谎言 提交于 2019-12-18 07:47:03
问题 Does anyone know if there's a de-facto standard (i.e., TR1 or Boost) C++ function object for accessing the elements of a std::pair? Twice in the past 24 hours I've wished I had something like the keys function for Perl hashes. For example, it would be nice to run std::transform on a std::map object and dump all the keys (or values) to another container. I could certainly write such a function object but I'd prefer to reuse something that's had a lot of eyeballs on it. 回答1: boost::bind is what

Are strtol, strtod unsafe?

余生颓废 提交于 2019-12-18 07:41:06
问题 It seems that strtol() and strtod() effectively allow (and force) you to cast away constness in a string: #include <stdlib.h> #include <stdio.h> int main() { const char *foo = "Hello, world!"; char *bar; strtol(foo, &bar, 10); // or strtod(foo, &bar); printf("%d\n", foo == bar); // prints "1"! they're equal *bar = 'X'; // segmentation fault return 0; } Above, I did not perform any casts myself. However, strtol() basically cast my const char * into a char * for me, without any warnings or

Why not modify key of associative container?

ぃ、小莉子 提交于 2019-12-18 05:53:37
问题 I know that it's a terrible idea to change the key of an object in an associative container, but I wonder where exactly the standard forbids me to do so. Consider: #include <map> #include <memory> struct X { int i; }; struct lt { bool operator()( const std::shared_ptr< X >& lhs, const std::shared_ptr< X >& rhs ) const { return lhs->i < rhs->i; } }; int main() { std::map< std::shared_ptr< X >, int, lt > m; auto x = std::make_shared< X >(); x->i = 1; m.insert( std::make_pair( x, 2 ) ); x->i =

Why Microsoft Visual Studio cannot find <stdint.h>? [duplicate]

守給你的承諾、 提交于 2019-12-18 05:49:28
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Visual Studio support for new C / C++ standards? See the text below from wiki: The C99 standard includes definitions of several new integer types to enhance the portability of programs[2]. The already available basic integer types were deemed insufficient, because their actual sizes are implementation defined and may vary across different systems. The new types are especially useful in embedded environments

Why is std:: used by experienced coders rather than using namespace std;? [duplicate]

放肆的年华 提交于 2019-12-18 05:45:30
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Why is 'using namespace std;' considered a bad practice in C++? The other day when I asked a question someone replied saying if someone asks a question, show them the right way to do it instead of using namespace std; which I thought was a bit weird, as using namespace std; is way easier, But I guess I'm failing right now as I am a 'beginner' coder and you guys know better. So I guess my question is: Why std::

Why is is_lock_free a member function?

ε祈祈猫儿з 提交于 2019-12-18 05:40:33
问题 What is the reason for why is_lock_free requires an instance (it's a member function)? Why not a metafunction of the type, or a static constexpr member function? I am looking for an actual instance of why it is necessary. 回答1: The standard allows a type to be sometimes lock-free. section 29.4 Lock-free property The ATOMIC_..._LOCK_FREE macros indicate the lock-free property of the corresponding atomic types, with the signed and unsigned variants grouped together. The properties also apply to

emplace_back() does not behave as expected

*爱你&永不变心* 提交于 2019-12-18 05:28:30
问题 I wrote a simple program to play around with in-place creation of objects inside standard library containers. This is what I wrote: #include <vector> #include <iostream> class AB { public: explicit AB(int n); AB(const AB& other) = delete; AB(AB&& other); AB& operator=(const AB& other) = delete; AB& operator=(AB&& other) = default; private: int i; }; AB::AB(int n): i( n ) { std::cout << "Object created." << std::endl; }; AB::AB(AB&& other): i( std::move(other.i) ) { std::cout << "Object moved.

Why does abs(complex<int>) always return zero?

╄→尐↘猪︶ㄣ 提交于 2019-12-18 04:33:29
问题 The following code with VS2010 prints 0 , contrary to my expectations: #include <complex> #include <iostream> using namespace std; int main(void) { complex<int> z(20, 200); cout << abs<int>(z) << endl; return 0; } It works fine when the type is double . 回答1: According to the C++ ISO spec, §26.2/2: The effect of instantiating the template complex for any type other than float , double or long double is unspecified. In other words, the compiler can do whatever it wants to when you instantiate