stdvector

Vector of vectors, reserve

巧了我就是萌 提交于 2019-11-29 05:29:30
Suppose I want to represent a two-dimensional matrix of int as a vector of vectors: std::vector<std::vector<int> > myVec; The inner dimension is constant, say 5, and the outer dimension is less than or equal to N . To minimize reallocations I would like to reserve space: myVec.reserve(N); What size is assumed for the inner vector? Is this purely implementation dependent? How does this effect the spatial locality of the data? Since the inner dimension is a constant is there a way to tell the compiler to use this constant size? How do these answers change if the inner vector's size changes?

Initialization of std::vector<unsigned int> with a list of consecutive unsigned integers

陌路散爱 提交于 2019-11-29 02:08:31
I want to use a special method to initialize a std::vector<unsigned int> which is described in a C++ book I use as a reference (the German book 'Der C++ Programmer' by Ulrich Breymann, in case that matters). In that book is a section on sequence types of the STL, referring in particular to list , vector and deque . In this section he writes that there are two special constructors of such sequence types, namely, if X refers to such a type, X(n, t) // creates a sequence with n copies of t X(i, j) // creates a sequence from the elements of the interval [i, j) I want to use the second one for an

Matlab API reading .mat file from c++, using STL container

ぐ巨炮叔叔 提交于 2019-11-29 01:56:51
问题 I have to read some .mat data files from c++, I read through the documentation, but I would like to know how to handle the data in a clean and elegant way, e.g. using std:vector(modest .mat file size(10M~1G), but memory issues should be taken seriously) My function is sth like: #include <stdio.h> #include "mat.h" #include <vector> int matread(const char *file, const vector<double>& pdata_v) { MATFile *pmat; pmat=matOpen("data.mat","r"); if (pmat == NULL) { printf("Error opening file %s\n",

std::vector as a template function argument

旧城冷巷雨未停 提交于 2019-11-28 21:21:53
I want to make a class method that takes a std::vector reference as an argument and I want to use it with different types of data. The function should look like: void some_function(const std::vector & vect){ //do something with vector } and I want use it with for example: std::vector<int> v1; some_function(v1); std::vector<string> v2; some_function(v2); I hope that I made my point clear. Do I have to make a template method like that: template<class T> void some_function(std::vector<T> & vect){} or can I do it in another way? If I have to, please tell me how I can write that method in a class.

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

我与影子孤独终老i 提交于 2019-11-28 20:15:13
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 ? 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. Note, that if the elements are pointers, the pointed-to objects are not destroyed. so no you don't have to

Initialisation of static vector

本秂侑毒 提交于 2019-11-28 20:07:41
I wonder if there is the "nicer" way of initialising a static vector than below? class Foo { static std::vector<int> MyVector; Foo() { if (MyVector.empty()) { MyVector.push_back(4); MyVector.push_back(17); MyVector.push_back(20); } } } It's an example code :) The values in push_back() are declared independly; not in array or something. Edit: if it isn't possible, tell me that also :) Typically, I have a class for constructing containers that I use (like this one from boost), such that you can do: const list<int> primes = list_of(2)(3)(5)(7)(11); That way, you can make the static const as well,

C++: Comparing two vectors

流过昼夜 提交于 2019-11-28 19:03:38
Is there any way to compare two vectors? if (vector1 == vector2) DoSomething(); Note: Currently, these vectors are not sorted and contain integer values. Jhaliya Check std::mismatch method of C++. comparing vectors has been discussed on DaniWeb forum and also answered . C++: Comparing two vectors Check the below SO post. will helpful for you. they have achieved the same with different-2 method. Compare two vectors C++ Your code ( vector1 == vector2 ) is correct C++ syntax. There is an == operator for vectors. If you want to compare short vector with a portion of a longer vector, you can use

std::vector<std::string> to char* array

不想你离开。 提交于 2019-11-28 18:15:27
I have a std::vector<std::string> that I need to use for a C function's argument that reads char* foo . I have seen how to convert a std::string to char* . As a newcomer to C++ , I'm trying to piece together how to perform this conversion on each element of the vector and produce the char* array. I've seen several closely related SO questions, but most appear to illustrate ways to go the other direction and create std::vector<std::string> . You can use std::transform as: std::transform(vs.begin(), vs.end(), std::back_inserter(vc), convert); Which requires you to implement convert() as: char

boost::python: Python list to std::vector

落花浮王杯 提交于 2019-11-28 17:31:45
问题 Finally I'm able to use std::vector in python using the [] operator. The trick is to simple provide a container in the boost C++ wrapper which handles the internal vector stuff: #include <boost/python.hpp> #include <vector> class world { std::vector<double> myvec; void add(double n) { this->myvec.push_back(n); } std::vector<double> show() { return this->myvec; } }; BOOST_PYTHON_MODULE(hello) { class_<std::vector<double> >("double_vector") .def(vector_indexing_suite<std::vector<double> >()) ;

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

北慕城南 提交于 2019-11-28 16:38:17
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; } ? In C++11, this is the preferred way: std::vector<X> f(); That is, return by value. With C++11, std::vector has move-semantics, which means the local vector declared in your