stdvector

Why erasing vector.end() is allowed?

浪子不回头ぞ 提交于 2019-12-12 07:02:12
问题 Today I was doing a bit of code, which looked something like this: vec.erase(std::remove_if(vec.begin(), vec.end(), <lambda here>)); When above code was not supposed to erase anything, meaning that std::remove_if should return vec.end(), I was very surprised when i received my vector with size decreased by one: last element was erased. Problem was fixed by changing above to: vec.erase(std::remove_if(vec.begin(), vec.end(), <lambda here>), vec.end()); But still question remains: how can vec

speeding vector push_back

半世苍凉 提交于 2019-12-12 05:41:24
问题 I am trying to speed vector::push_back when capacity cant be predicted When reserve is available a vector push_back writes the new element at the end of the container, and then the end marker is moved. After all reserve is used, push_back may trigger reallocation which is a slow process. To speed this up, reserve is regenerated for several coming push_back without reallocation when empty. How do you think this code assist in achieving that goal ? #ifndef __VECTOR_HPP #define __VECTOR_HPP

std::list implementation & pointer arithemetic.

纵饮孤独 提交于 2019-12-12 04:56:30
问题 As I understand it, std::vector allocates/de-allocates all the memory it requires each time it's elements grows or shrinks, therefore pointer arithmetic can be used to iterate the vector elements. std::list on the other hand uses a double linked list, with each element pointing to the next and previous element. Assuming(possibly wrongly) that std::list allocates it's memory dynamically, so memory is allocated, if and when required, incrementally. How is std::list still able to offer pointer

std::unique with predicate comparing std::string not removing duplicate

强颜欢笑 提交于 2019-12-12 03:49:46
问题 Unless I am missing something or missunderstand the mechanism (very likely) Shouldn't the "1" duplicate not exist in this vector ? chunks.erase( std::unique ( chunks.begin(), chunks.end(), []( std::string &s1, std::string &s2 ){ return ( s1.compare(s2) == 0 ? true : false );}), chunks.end() ); Before Executing the above: 1 l:1 1+ l:2 1+1 l:3 1+1= l:4 + l:1 +1 l:2 +1= l:3 1 l:1 1= l:2 = l:1 After executing the above code: 1 l:1 1+ l:2 1+1 l:3 1+1= l:4 + l:1 +1 l:2 +1= l:3 1 l:1 1= l:2 = l:1 I

How to connect two elements of a vector?

自作多情 提交于 2019-12-12 01:54:27
问题 I would like to know if there is a way in C++ to connect two elements of a vector for example std::vector such that if one is changed, the other changes automatically. If not is there any other way to do so? Thanks. 回答1: Let's say you have two vectors containing instances of a same kind of Object . Then using shared_ptr<Object> you can refer to the same object: vector<shared_ptr<Object>> v1; vector<shared_ptr<Object>> v2; for(int i=0;i<3;i++) { v1.push_back(shared_ptr<Object>(new Object()));

Can a C++ vector of non-pointer types cause a memory leak in iOS?

跟風遠走 提交于 2019-12-12 01:28:46
问题 Here's the code: @interface myClass { std::vector<float> myVector } @end It's leaking according to instruments. Here's the stack trace: 1 libstdc++.6.dylib operator new(unsigned long) 2 __gnu_cxx::new_allocator<float>::allocate(unsigned long, void const*) 3 std::_Vector_base<float, std::allocator<float> >::_M_allocate(unsigned long) I'm guessing I should be allocating the vector on the heap, but I still don't understand why this occurs. It's also possible I'm failing to dealloc the class

C++ std::vector::size() changes its state

一曲冷凌霜 提交于 2019-12-12 00:02:40
问题 I'm relatively new to c++ and am confused by some strange behavior. I get an object which contains an std::vector. Then, I print out its size twice, by exactly the same copied line: Pose& pose = getCurrentPose(); std::cout << "nr1: " << pose.transforms.size() << " bones." << std::endl; std::cout << "nr2: " << pose.transforms.size() << " bones." << std::endl; The result: Nr1: 17 bones. Nr2: 38294074 bones. Any further calls to this vector's size returns the same huge number (17 should be right

How dynamically filled multidimensional vector without a priori known its dimension c++

家住魔仙堡 提交于 2019-12-11 17:21:38
问题 I'm writing a class for tensors, I need it for my work. I figure out how to create dynamic multidimensional vector using recursion, but I don't know how to stretch it to dimensional size what I need (like this vector<double> vec; vec.resize(6); ). But I want to get dynamic vector and have created template class. Could someone help me with it? This is my code: template<typename T,size_t N> struct tensor_traits { using type = vector< typename tensor_traits<T,N-1>::type>; }; template<typename T>

Why's my vector element all garbage?

家住魔仙堡 提交于 2019-12-11 16:01:43
问题 I have a GameLobby class that keeps a list of the currently active games. I want to fetch the active games from a GameLobby (singleton) object, and display these to the user. (Disclaimer: I'm pretty new to C++, so the following code isn't exactly stellar. Neither is it the complete code, but I feel confident that all the relevant instructions have been included.) First some definitions class GamesMenu : public MyGameLayer { private: std::vector<Game*>* _activeGames; void displayGamesList();

How to split a hex string into std::vector?

一曲冷凌霜 提交于 2019-12-11 10:08:50
问题 Given a hex std::string like "09e1c5f70a65ac519458e7e53f36" , how can I split it in chunks of two digits and store them into an std::vector<uint8_t> ? I loop over the string in steps of the chunk size, but I don't know how to convert the hex chunk into a number. This is what I have so far. vector<byte> vectorify(string input, int chunk = 2) { vector<uint8_t> result; for(size_t i = 0; i < input.length(); i += chunk) { int hex = input.substr(i, chunk); // ... } return result; } 回答1: #include