stl

Storing hexadecimal values from a std::string in a std::vector and vise versa?

拥有回忆 提交于 2020-01-01 06:37:32
问题 Let's say you've got a std::string("DEADBEEF") , what would be the most elegant way to store each two values in a std::vector (most likely a std::vector<unsigned char> , if possible) so the std::vector would look a little like { 0xDE, 0xAD, 0xBE, 0xEF } (if it was an array)? And what would be the nicest way to undo that ( std::vector<unsigned char> -> std::string ). 回答1: how about (no fancy stl juggling though): #include <string> #include <vector> // exception thrown by the encoder class

Using std::bind2nd with references

若如初见. 提交于 2020-01-01 06:27:17
问题 I have a simple class like this: class A { public: void f(const int& n) { std::cout<<"A::f()" << n <<"\n"; } }; and I am trying to use it like this: std::vector<A> vec; A a; vec.push_back(a); std::for_each(vec.begin(), vec.end(), std::bind2nd(std::mem_fun_ref(&A::f), 9)); But when I compile the code I get the following error somewhere inside functional header file: error C2529: '_Right' : reference to reference is illegal If I remove the reference in the parameter f() it compiles fine. How do

Adding template specialization in std namespace

蹲街弑〆低调 提交于 2020-01-01 05:48:13
问题 Background: I tried to answer the question Why isn't my overloading < operator not working for STL sort. One of my suggestion (apart from using predicate) was to move the custom operator < for std::string in namespace std so that it can be preferred by the compiler over templated version. At lightening speed the answer was down-voted with following comment from a highly reputed user: This is undefined behaviour, you are not allowed to add declarations to namespace std because it can change

Call member function on each element in a container

独自空忆成欢 提交于 2020-01-01 04:54:06
问题 This question is a matter of style, since you can always write a for loop or something similar; however, is there a less obtrusive STL or BOOST equivalent to writing: for (container<type>::iterator iter = cointainer.begin(); iter != cointainer.end(); iter++) iter->func(); ? Something like (imagined) this: call_for_each(container.begin(), container.end(), &Type::func); I think it would be 1) less typing, 2) easier to read, 3) less changes if you decided to change base type/container type. EDIT

Template arguments to template functions

守給你的承諾、 提交于 2020-01-01 04:46:06
问题 I just released a skiplist container library. And the Sun compiler complains about this: template <class T, class R> bool operator==(const IndexedSkipList<T,R> &left, const IndexedSkipList<T,R> &right) { return ((left.size() == right.size()) && (std::equal(left.begin(), left.end(), right.begin()))); } The errors are: "include/CSIndexedSkipList.h", line 65: Error: Too few arguments for template std::reverse_iterator<CS::BidiIdxIterator<CS::IndexedSkipList<CS::T, CS::R>>>. "include

EASTL versus STL, how can there be such a performance difference in std::vector<uint64_t>::operator[]

☆樱花仙子☆ 提交于 2020-01-01 04:18:05
问题 According to http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2271.html vector<uint64>::operator[] is between 2% and 70% faster in EASTL than a "commonly used commercial version of STL". Unless the commercial version of STL uses range checking, which would make the comparison unfair, how can it possibly be such a speed difference for such a simple operation? Update: Seems the answer is that the EA engineers is simply cheating by comparing with a version which uses range checking...

std::string operator+() memory leak?

喜欢而已 提交于 2020-01-01 04:17:15
问题 I'm quite worry because I wrote a little application and it seems that there is a memory leak if I believe valgrind (What I actually do): ==9321== 251 bytes in 7 blocks are definitely lost in loss record 1 of 1 ==9321== at 0x402569A: operator new(unsigned int) (vg_replace_malloc.c:255) ==9321== by 0x40D3D05: std::string::_Rep::_S_create(unsigned int, unsigned int, std::allocator<char> const&) (in /usr/lib/libstdc++.so.6.0.13) ==9321== by 0x40D4977: std::string::_Rep::_M_clone(std::allocator

c++ less operator overload, which way to use?

旧城冷巷雨未停 提交于 2020-01-01 04:04:06
问题 For example: in a C++ header file, if I defined a struct Record and I would like to use it for possible sorting so that I want to overload the less operator . Here are three ways I noticed in various code. I roughly noticed that: if I'm going to put Record into a std::set , map , priority_queue , … containers, the version 2 works (probably version 3 as well); if I'm going to save Record into a vector<Record> v and then call make_heap(v.begin(), v.end()) etc.. then only version 1 works. struct

std::list iterator: get next element

邮差的信 提交于 2020-01-01 02:47:09
问题 I'm trying to build a string using data elements stored in a std::list, where I want commas placed only between the elements (ie, if elements are {A,B,C,D} in list, result string should be "A,B,C,D". This code does not work: typedef std::list< shared_ptr<EventDataItem> > DataItemList; // ... std::string Compose(DataItemList& dilList) { std::stringstream ssDataSegment; for(iterItems = dilList.begin(); iterItems != dilList.end(); iterItems++) { // Lookahead in list to see if next element is end

Stream from std::string without making a copy?

故事扮演 提交于 2020-01-01 02:43:08
问题 I have a network client with a request method that takes a std::streambuf* . This method is implemented by boost::iostreams::copy -ing it to a custom std::streambuf -derived class that knows how to write the data to a network API, which works great. This means I can stream a file into the request without any need to read it all into memory. There are some cases, however, where large blocks of data must be sent which are not in a file, so I included an overload that takes a string. To avoid