stl

Get pointer to node in std::list or std::forward_list

与世无争的帅哥 提交于 2020-07-15 08:29:20
问题 I am planning to use std::list in my code, I decided not to use std::forward_list, because for deletions (I figured) the whole list will have to traversed, O(N) complexity for std::forward_list (being a single link list). However, when I looked into the documentation I noticed both the stl containers have O(N) complexity to remove an item. http://www.cplusplus.com/reference/forward_list/forward_list/remove/ http://www.cplusplus.com/reference/list/list/remove/ After some thinking I figured out

std::array or std::vector from pointer

我的未来我决定 提交于 2020-07-10 07:17:17
问题 I have an array of data in a C++/CLI array that I can pass to a native function using pin_ptr<T> , no problem so far. Now, however, I need to pass the array on to a C++/STL function that expects a container such as std::array or std::vector . The easy way of doing this (which I did first), is to copy element by element. The second-easiest way is to call std::copy() , see the answer to this question: convert System::array to std::vector. However, I want to skip the entire copying step and just

std::array or std::vector from pointer

痞子三分冷 提交于 2020-07-10 07:16:04
问题 I have an array of data in a C++/CLI array that I can pass to a native function using pin_ptr<T> , no problem so far. Now, however, I need to pass the array on to a C++/STL function that expects a container such as std::array or std::vector . The easy way of doing this (which I did first), is to copy element by element. The second-easiest way is to call std::copy() , see the answer to this question: convert System::array to std::vector. However, I want to skip the entire copying step and just

Determine if there are duplicates in vector

时光怂恿深爱的人放手 提交于 2020-07-09 13:47:09
问题 I would like to determine if there are duplicates in a vector. What is the best option here? sort(arrLinkToClients.begin(), arrLinkToClients.end(), [&](const type1& lhs,const type1& rhs) { lhs.nTypeOfLink < rhs.nTypeOfLink; }); auto it = unique(arrLinkToClients.begin(), arrLinkToClients.end(), [&](const type1& lhs, const type1& rhs) { lhs.nTypeOfLink == rhs.nTypeOfLink; }); //how to check the iterator if there are duplicates ? if (it) { // } 回答1: The "best" option does not exist. It depends

Is clamping on iterators valid

妖精的绣舞 提交于 2020-07-08 11:49:02
问题 I found the following in actual production code. My suspicion is that it actually has undefined behavior into it, however, I couldn't find the related info on cppreference. Can you confirm this is UB or valid code and why this is UB/valid (preferably with a quote of the standard)? #include <vector> int main(int, char **) { auto v = std::vector<int>({1,2,3,4,5}); auto begin = v.begin(); auto outOfRange = begin + 10; auto end = v.end(); auto clamped = std::min(outOfRange, end); return (clamped

Why can we not access elements of a tuple by index?

感情迁移 提交于 2020-07-04 20:24:15
问题 tuple <int, string, int> x=make_tuple(1, "anukul", 100); cout << x[0]; //1 cout << get<0>(x); //2 2 works. 1 does not. Why is it so? From Lounge C++ I learnt that it is probably because the compiler does not know what data type is stored at that index. But it did not make much sense to me as the compiler could just look up the declaration of that tuple and determine the data type or do whatever else is done while accessing other data structures' elements by index. 回答1: Because [] is an

Can I initialize an STL vector with 10 of the same integer in an initializer list?

。_饼干妹妹 提交于 2020-07-04 06:07:50
问题 Can I initialize an STL vector with 10 of the same integer in an initializer list? My attempts so far have failed me. 回答1: I think you mean this: struct test { std::vector<int> v; test(int value) : v( 100, value ) {} }; 回答2: Use the appropriate constructor, which takes a size and a default value. int number_of_elements = 10; int default_value = 1; std::vector<int> vec(number_of_elements, default_value); 回答3: If you're using C++11 and on GCC, you could do this: vector<int> myVec () {[0 ... 99]

How to identify whether or not std::unordered_map has experienced hash collisions?

依然范特西╮ 提交于 2020-07-03 06:51:30
问题 How to identify whether or not the keys in a std::unordered_map have experienced hash collisions? That is, how to identify if any collision chaining is present? 回答1: You can use the bucket interface and its bucket_size method. std::unordered_map<int, int> map; bool has_collision = false; for(size_t bucket = 0; bucket < map.bucket_count(); bucket++) { if(map.bucket_size(bucket) > 1) { has_collision = true; break; } } 来源: https://stackoverflow.com/questions/46137811/how-to-identify-whether-or

How to identify whether or not std::unordered_map has experienced hash collisions?

笑着哭i 提交于 2020-07-03 06:50:20
问题 How to identify whether or not the keys in a std::unordered_map have experienced hash collisions? That is, how to identify if any collision chaining is present? 回答1: You can use the bucket interface and its bucket_size method. std::unordered_map<int, int> map; bool has_collision = false; for(size_t bucket = 0; bucket < map.bucket_count(); bucket++) { if(map.bucket_size(bucket) > 1) { has_collision = true; break; } } 来源: https://stackoverflow.com/questions/46137811/how-to-identify-whether-or

STL set_symmetric_difference usage

时间秒杀一切 提交于 2020-06-29 11:14:29
问题 I was solving a programming problem, which wants to find the SYMMETRIC DIFFERENCE between two sets. I have solved it using STL's set_symmetric_difference . I am given two vector<int>s , A and B : A = {342,654,897,312,76,23,78} B = {21,43,87,98,23,756,897,234,645,876,123} Sould return (correct answer): { 21,43,76,78,87,98,123,234,312,342,645,654,756,876 } But I get: { 21,43,76,78,87,98,123,234,312,342,645,65,756,876} What is the problem ? Here is my code: sort(A.begin(), A.end()); sort(B.begin