stdvector

C++ reference changes when push_back new element to std::vector

时光怂恿深爱的人放手 提交于 2019-12-17 19:10:57
问题 I am not sure what to make of this - please tell me what's wrong with the code below. I modified my code to reduce it to the simplest terms. There is a std::vector with a bunch of MyNode objects. The first step is to get a constant reference to one of the data elements of one of these nodes (Data m_data) - in the example below, there is only one node before the 2nd node is inserted as seen below: const cv::Data& currData = m_nodesVector[currIndex].GetData(); MyNode node(...); m_nodesVector

STL vector: Moving all elements of a vector

青春壹個敷衍的年華 提交于 2019-12-17 17:47:41
问题 I have two STL vectors A and B and I'd like to clear all elements of A and move all elements of B to A and then clear out B . Simply put, I want to do this: std::vector<MyClass> A; std::vector<MyClass> B; .... A = B; B.clear(); Since B could be pretty long, it takes k*O(N) to do this operation, where k is a constant, and N is max(size_of(A), size_of(B)) . I was wondering if there could be a more efficient way to do so. One thing that I could think of is to define A and B as pointers and then

In C++ check if std::vector<string> contains a certain value [duplicate]

核能气质少年 提交于 2019-12-17 17:42:17
问题 This question already has answers here : How to find out if an item is present in a std::vector? (19 answers) Closed 6 years ago . Is there any built in function which tells me that my vector contains a certain element or not e.g. std::vector<string> v; v.push_back("abc"); v.push_back("xyz"); if (v.contains("abc")) // I am looking for one such feature, is there any // such function or i need to loop through whole vector? 回答1: You can use std::find as follows: if (std::find(v.begin(), v.end(),

Fastest way to reset every value of std::vector<int> to 0

匆匆过客 提交于 2019-12-17 17:20:16
问题 What's the fastest way to reset every value of a std::vector<int> to 0 and keeping the vectors initial size ? A for loop with the [] operator ? 回答1: std::fill(v.begin(), v.end(), 0); 回答2: As always when you ask about fastest: Measure! Using the Methods above (on a Mac using Clang): Method | executable size | Time Taken (in sec) | | -O0 | -O3 | -O0 | -O3 | ------------|---------|---------|-----------|----------| 1. memset | 17 kB | 8.6 kB | 0.125 | 0.124 | 2. fill | 19 kB | 8.6 kB | 13.4 | 0

std::vector resize downward

大城市里の小女人 提交于 2019-12-17 09:35:42
问题 The C++ standard seems to make no statement regarding side-effects on capacity by either resize(n) , with n < size() , or clear() . It does make a statement about amortized cost of push_back and pop_back - O(1) I can envision an implementation that does the usual sort of capacity changes ala CLRS Algorithms (e.g. double when enlarging, halve when decreasing size to < capacity()/4 ). (Cormen Lieserson Rivest Stein) Does anyone have a reference for any implementation restrictions? 回答1: Calling

May std::vector make use of small buffer optimization?

只谈情不闲聊 提交于 2019-12-17 04:59:17
问题 I was wondering with my colleague today whether std::vector can be implemented to make use of small buffer optimization. By looking into the C++11 draft, I read at 23.3.1p8 The expression a.swap(b), for containers a and b of a standard container type other than array, shall exchange the values of a and b without invoking any move, copy, or swap operations on the individual container elements. That at first seems to outlaw small buffer optimization, but under the as-if rule, we would be

Nice way to append a vector to itself

醉酒当歌 提交于 2019-12-17 02:31:42
问题 I want to duplicate the contents of the vector and want them to be appended at the end of the original vector i.e. v[i]=v[i+n] for i=0,2,...,n-1 I am looking for a nice way to do it, not with a loop. I saw std::vector::insert but the iterative version forbids a iterator to *this (i.e behaviour is undefined). I also tried std::copy as follows(but it resulted in segmentation fault): copy( xx.begin(), xx.end(), xx.end()); 回答1: Wow. So many answers that are close, none with all the right pieces.

Read a file in C++ from a specific Line and then store it in Vectors/Array

可紊 提交于 2019-12-14 03:28:35
问题 I am new to C++ programming and I did my homework for reading a file. I am learning C++ from this cpp-tutorial: Basic-file-io site. I have a file whose content looks like: Input: /path/to/the/file/ Information :xxx Type of File: Txt file Extra Information Value = 4 Development = 55 NId CommId 1 0 3 0 8 7 . . And so on... This file has about 10000 Nodes and their corresponding CommID . The Node and CommId are seperated by TAb space in this file. I am reading this file as Input by using the

Enforcing one-at-a-time access to pointer from a primative wrapper

风格不统一 提交于 2019-12-14 03:19:43
问题 I've read a fair amount on thread-safety, and have been using GCD to keep the math-heavy code off the main thread for a while now (I learned about it before NSOperation, and it seems to still be the easier option). However, I wonder if I could improve part of my code that currently uses a lock. I have an Objective-C++ class that is a wrapper for a c++ vector. (Reasons: primitive floats are added constantly without knowing a limit beforehand, the container must be contiguous, and the reason

C++ : How to detect duplicates in vector<string> and print ONE copy?

不打扰是莪最后的温柔 提交于 2019-12-14 01:32:26
问题 I'm new to C++. I was wondering how I can find duplicate strings in a vector and print out ONE copy of the string. For example, if I had <"cat", "dog", "dog", "bird",> it would print out cat, dog, bird. I have sorted my vector and am using the adjacent_find function and iterating through the vector (since I have to find if any word is duplicated). My code detects duplicates, but it only prints out the non-duplicates. I would like to alter it to print out all the non-duplicates and also just