stdvector

Should I always call vector clear() at the end of the function?

假装没事ソ 提交于 2019-11-27 12:48:48
问题 I have some simple function that uses vector like this (pseudo code): void someFunc(void) { std::vector<std::string> contentVector; // here are some operations on the vector // should I call the clear() here or this could be ommited ? contentVector.clear(); } Should I call the clear() or this could be ommited ? 回答1: If we look at the cppreference.com entry for std::vector::~vector it says: Destructs the container. The destructors of the elements are called and the used storage is deallocated.

Is there a standard way of moving a range into a vector?

雨燕双飞 提交于 2019-11-27 11:45:54
Consider the following program which inserts a range of elements into a vector: vector<string> v1; vector<string> v2; v1.push_back("one"); v1.push_back("two"); v1.push_back("three"); v2.push_back("four"); v2.push_back("five"); v2.push_back("six"); v1.insert(v1.end(), v2.begin(), v2.end()); This efficiently copies the range, allocating enough space in the target vector for the entire range so that a maximum of one resize will be required. Now consider the following program which attempts to move a range into a vector: vector<string> v1; vector<string> v2; v1.push_back("one"); v1.push_back("two"

Converting between C++ std::vector and C array without copying

好久不见. 提交于 2019-11-27 10:55:39
I would like to be able to convert between std::vector and its underlying C array int* without explicitly copying the data. Does std::vector provide access to the underlying C array? I am looking for something like this vector<int> v (4,100) int* pv = v.c_array(); EDIT: Also, is it possible to do the converse, i.e. how would I initialize an std::vector from a C array without copying? int pv[4] = { 4, 4, 4, 4}; vector<int> v (pv); You can get a pointer to the first element as follows: int* pv = &v[0]; This pointer is only valid as long as the vector is not reallocated. Reallocation happens

Efficient way to return a std::vector in c++

老子叫甜甜 提交于 2019-11-27 09:43:36
问题 How much data is copied, when returning a std::vector in a function and how big an optimization will it be to place the std::vector in free-store (on the heap) and return a pointer instead i.e. is: std::vector *f() { std::vector *result = new std::vector(); /* Insert elements into result */ return result; } more efficient than: std::vector f() { std::vector result; /* Insert elements into result */ return result; } ? 回答1: In C++11, this is the preferred way: std::vector<X> f(); That is,

How do I sort the texture positions based on the texture indices given in a Wavefront (.obj) file?

元气小坏坏 提交于 2019-11-27 09:33:08
I'm currently trying to make a Wavefront (.obj) file loader for an OpenGL project. The method I'm currently using goes line-by-line and separates the vertex positions, texture positions and normal positions in vectors (std::vectors) and I'm storing their indices (vertex, texture and normal indices) in three separate vectors (from the 'f' lines of the file, for each face). I'm having trouble sorting the vector full of texture coordinates based on the texture indices. I'm able to render the vertices in the correct positions because my 'loader' class calls for the indices, but I can't figure out

Unresolved externals in C++ when using vectors and find

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 09:02:48
I have tried this code in a totally separate project, and it works fine (the only difference being that the project that is not working is being exported as a DLL). Here is the code: RTATMATHLIB.CPP #include "stdafx.h" #include "RTATMATHLIB.h" #include <math.h> #include <vector> #include <algorithm> #include <stdexcept> using namespace std; double someFunc(double** Y, int length) { vector<double> myVector; for(int i = 0; i < length; i++) { double value = (*Y)[i]; vector<double>::iterator it = find(myVector.begin(), myVector.end(), value); if(it != myVector.end()) { continue; } else { myVector

Getting a bool reference from std::vector<bool>

六月ゝ 毕业季﹏ 提交于 2019-11-27 08:31:27
问题 I know it's a bad habit, but I'd like to know some workaround or hack for this problem. I have a class like this: template <class T> class A : std::vector<T> { T& operator()(int index) { // returns a _reference_ to an object return this->operator[](index); } }; It's possible to do things like this: A<int> a{1,2,3,4}; a(3) = 10; But it stops working if somebody uses bool as a template parameter A<bool> a{true, false, true}; std::cout << a(0) << std::endl; // not possible if (a(1)) { /*

Why does reallocating a vector copy instead of moving the elements? [duplicate]

房东的猫 提交于 2019-11-27 08:00:00
Possible Duplicate: How to enforce move semantics when a vector grows? insert , push_back and emplace ( _back ) can cause a reallocation of a std::vector . I was baffled to see that the following code copies the elements instead of moving them while reallocating the container. #include <iostream> #include <vector> struct foo { int value; explicit foo(int value) : value(value) { std::cout << "foo(" << value << ")\n"; } foo(foo const& other) noexcept : value(other.value) { std::cout << "foo(foo(" << value << "))\n"; } foo(foo&& other) noexcept : value(std::move(other.value)) { other.value = -1;

std::vector resize downward

佐手、 提交于 2019-11-27 07:59:13
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? mattnewport Calling resize() with a smaller size has no effect on the capacity of a vector . It will not free memory

Are std::map and std::vector thread safe?

孤街醉人 提交于 2019-11-27 07:49:29
问题 I am developing a multi threaded application, each thread will read (there will be no modifying of structures) from a group of maps and vectors. Can anyone please advise, since the threads are only reading from these structures would it be necessary to implement a sharable mutex around the code blocks where these structures are being read? 回答1: In case of read only map/vector there is no need to use mutexes. This was already answered for both vector and map While C++03 doesn't mention threads