vector

Find index of last occurrence for each value

筅森魡賤 提交于 2021-02-16 20:01:53
问题 I have a vector like this : >> v = [1 1 1 2 2 3 4 4 4 4 4 5 5]' v = 1 1 1 2 2 3 4 4 4 4 4 5 5 The vector is sorted. There can be any number of each values. I need to find the index of the last occurence of each value. In this case, it would return this : answer = 3 % index of the last occurence of "1" 5 % index of the last occurence of "2" 6 % index of the last occurence of "3" 11 % index of the last occurence of "4" 13 % index of the last occurence of "5" 回答1: Thanks to @trumpetlicks, the

Find index of last occurrence for each value

此生再无相见时 提交于 2021-02-16 20:01:19
问题 I have a vector like this : >> v = [1 1 1 2 2 3 4 4 4 4 4 5 5]' v = 1 1 1 2 2 3 4 4 4 4 4 5 5 The vector is sorted. There can be any number of each values. I need to find the index of the last occurence of each value. In this case, it would return this : answer = 3 % index of the last occurence of "1" 5 % index of the last occurence of "2" 6 % index of the last occurence of "3" 11 % index of the last occurence of "4" 13 % index of the last occurence of "5" 回答1: Thanks to @trumpetlicks, the

How to remove all instances of a duplicate from a vector<int> [duplicate]

余生颓废 提交于 2021-02-16 17:57:12
问题 This question already has answers here : How to remove duplicated items in a sorted vector (7 answers) Closed yesterday . I've been trying to solve an "easy" leetcode question for two hours now where I need to remove every instance of an int that appears more than once then add the non dupes together. I've tried about 10 different ways and I can only get it to go down to one copy of each int. Here is the nicest solution I've written, but given input {1,2,2,3,4} it would return {1,2,3,4}

Using Binary Search with Vectors.

﹥>﹥吖頭↗ 提交于 2021-02-16 08:55:32
问题 I'm trying to implement an algorithm that for each string in the first vector it does a binary search in the second vector and will output "YES:" if it finds a match or "No:" otherwise. Right now with my program my algo always outputs "NO:" and I can't find out what's going wrong. Any hints or tips would be appreciated. My Binary search: bool binary_search(const vector<string>& sorted_vec, string key) { size_t mid, left = 0 ; size_t right = sorted_vec.size(); // one position passed the right

Using Binary Search with Vectors.

不打扰是莪最后的温柔 提交于 2021-02-16 08:55:17
问题 I'm trying to implement an algorithm that for each string in the first vector it does a binary search in the second vector and will output "YES:" if it finds a match or "No:" otherwise. Right now with my program my algo always outputs "NO:" and I can't find out what's going wrong. Any hints or tips would be appreciated. My Binary search: bool binary_search(const vector<string>& sorted_vec, string key) { size_t mid, left = 0 ; size_t right = sorted_vec.size(); // one position passed the right

shrink_to_fit() vs swap trick

强颜欢笑 提交于 2021-02-16 05:54:14
问题 I have a game where certain game objects spawn all at once and then despawn as they get destroyed/killed. The game objects are elements in an std::vector , and I'd like to minimize memory usage. I'm used to the swap trick, std::vector<gameObject>(gameObjectVector.begin(), gameObjectVector.end()).swap(gameObjectVector); but I noticed the inbuilt shrink_to_fit() from C++11. However, it has linear complexity while the swap trick is constant. Isn't the swap trick superior in every way? 回答1: The

Find string in vector of strings case insensitive c++

不打扰是莪最后的温柔 提交于 2021-02-15 05:57:46
问题 I have std::vector<std::string> vec; std::string myString; and I need to find out if myString is in vec using case insensitive comaprisons. I know I can use find(vec.begin(), vec.end(), myString) != vec.end()) to answer the question "is myString in vec?" but that will do case sensitive comparisons. I need case insensitive comparisons. The position is not important, I just want to know if myString is in vec or not. 回答1: Or, for a much smaller and easier-to-read solution, Boost! // #include

Find string in vector of strings case insensitive c++

◇◆丶佛笑我妖孽 提交于 2021-02-15 05:57:15
问题 I have std::vector<std::string> vec; std::string myString; and I need to find out if myString is in vec using case insensitive comaprisons. I know I can use find(vec.begin(), vec.end(), myString) != vec.end()) to answer the question "is myString in vec?" but that will do case sensitive comparisons. I need case insensitive comparisons. The position is not important, I just want to know if myString is in vec or not. 回答1: Or, for a much smaller and easier-to-read solution, Boost! // #include

How to implement erase on vector in c++

大憨熊 提交于 2021-02-11 18:15:59
问题 Learning from Accelerated C++: Practical Programming by Example , in chapter 11, there was an implementation (only with basic features) of vector container from STL. After that was an exercise for implementing erase function just as std::vector does. What I have tried: #include <memory> template<class T> class Vec{ private: T *data; T *avail; T *limit; std::allocator<T> alloc; ... public: explicit Vec(size_t n, const T &val = T()) { create(n, val); } T *const begin() { return data; } T *const

C++ nested for loop with erasing elements

荒凉一梦 提交于 2021-02-11 14:14:20
问题 I would like to check all Elements of an vector against each other. By checking a condition an element should be removed. One approach was to erase the elements by nested for loops for (int a = 0; a < rs.size(); a++) { Point A = rs[a]; for (int b = 1; b <= rs.size(); b++) { Point B = rs2[b]; float distance = sqrt(pow(B.x - A.x, 2) + pow(B.y - A.y, 2) * 1.0); if (distance < 10.0) { if (distance > 0) { rs.erase(rs.begin() + b); } } } } but this would effect the vector and his size in runtime. A