stdvector

How to define a 2D array in C++ and STL without memory manipulation?

纵然是瞬间 提交于 2019-12-03 03:45:27
There are several ways to define a 2D array in C++ and STL without memory manipulation, and the following codes illustrate two different methods: int main () { /************** 1 2 3 4 5 6 ***************/ // Method 1 const int ROW = 2; const int COL = 3; int array1[ROW][COL]; for(int i=0; i<ROW; i++) for(int j=0; j<COL; j++) array1[i][j] = i*COL+j+1; // Method 2 typedef vector<vector<int> > ARRAY; ARRAY array2; vector<int> rowvector; for(int i=0; i<ROW; i++) { rowvector.clear(); for(int j=0; j<COL; j++) rowvector.push_back(i*COL+j+1); array2.push_back(rowvector); } return 0; } My question is:

Proper way of transferring ownership of a std::vector< std::unique_ptr< int> > to a class being constructed

那年仲夏 提交于 2019-12-03 01:21:02
What is the proper way of transferring ownership of a std::vector<unique_ptr<int> > to a class being constructed? Below is a code representation of what I want to do. I realize it is not correct (won't compile) and violates "uniqueness" whether I pass the vector to the constructor by value or by reference. I want Foo to be the new owner of the vector, and want the calling function to relinquish ownership. Do I need the constructor to take a std::unique_ptr<std::vector<std::unique_ptr<int> > > to do this? Foo.h class Foo { public: Foo(vector<std::unique_ptr<int> > vecOfIntPtrsOwnedByCaller);

Vector storage in C++

主宰稳场 提交于 2019-12-03 00:58:53
I wish to store a large vector of d-dimensional points (d fixed and small: <10). If I define a Point as vector<int> , I think a vector<Point> would store in each position a pointer to a Point. But if define a Point as a fixed-size object like: std::tuple<int,int,...,int> or std::array<int, d> , will the program store all points in contiguous memory or will the additional level of indirection remain? In case the answer is that arrays avoid the additional indirection, could this have a large impact on performance (cache exploit locality) while scanning the vector<Point> ? If you define your

Why does compiling over 100,000 lines of std::vector::push_back take a long time?

拟墨画扇 提交于 2019-12-02 18:15:06
I'm compiling a C++ library which defines a single function that randomly samples from a set of data points. The data points are stored in a std::vector . There are 126,272 std::vector push_back statements, where the vector in question is of type double . It is taking a long time to compile. Why would this take so long? (All the code other than the std::vector push_back statements would take less than 1 second to compile, because there's very little other code.) osgx There is the -ftime-report option in gcc which prints the detailed report of time wasted by each compiler phase. I'm used ubuntu

std::map<int, int> vs. vector of vector

怎甘沉沦 提交于 2019-12-02 17:54:32
问题 I need a container to store a value (int) according to two attributes, source (int) and destination (int) i.e. when a source sends something to a destination, I need to store it as an element in a container. The source is identified by a unique int ID (an integer from 0-M), where M is in the tens to hundreds, and so is the destination (0-N). The container will be updated by iterations of another function. I have been using a vector(vector(int)) which means goes in the order of source

advantages of std::set vs vectors or maps

自古美人都是妖i 提交于 2019-12-02 17:53:52
This may be a stupid question, I am quite new to C++ and programming in general. I wish to understand the use of several STL containers and with that in mind, I was wondering what the advantages are of using std::set vs for example using vectors or maps? I can't seem to find an explicit answer to this question. I noticed that sets use maps, but then why not always use maps or always use sets. Instead 2 quite similar containers are provided. Thanks in advance. Both std::set and std::map are associative containers. The difference is that std::set s contain only the key, while in std::map there

C++ One std::vector containing template class of multiple types

流过昼夜 提交于 2019-12-02 17:26:12
I need to store multiple types of a template class in a single vector. Eg, for: template <typename T> class templateClass{ bool someFunction(); }; I need one vector that will store all of: templateClass<int> t1; templateClass<char> t2; templateClass<std::string> t3; etc As far as I know this is not possible, if it is could someone say how? If it isn't possible could someone explain how to make the following work? As a work around I tried to use a base, non template class and inherit the template class from it. class templateInterface{ virtual bool someFunction() = 0; }; template <typename T>

Cast of std::vector of same parameter type but with different constant qualifier

三世轮回 提交于 2019-12-02 13:20:01
the question is pretty simple, is it in general safe a static cast (or some other cast) from std::vector< Foo > to std::vector< const Foo > binary-wise, i don't see why the native types would differ, after all, the const is a language constraint that should not affect the size of the element, or so i think can i do std::vector< const Foo >& someFunc() { std::vector< Foo >& ref = ... return *reinterpret_cast<std::vector< const Foo >*>(& ref); } and not worry that this will sink someone's boat? or is this unsafe in general? Bowie Owens Ignoring that you said std::vector for the moment and

std::map<int, int> vs. vector of vector

喜夏-厌秋 提交于 2019-12-02 11:39:05
I need a container to store a value (int) according to two attributes, source (int) and destination (int) i.e. when a source sends something to a destination, I need to store it as an element in a container. The source is identified by a unique int ID (an integer from 0-M), where M is in the tens to hundreds, and so is the destination (0-N). The container will be updated by iterations of another function. I have been using a vector(vector(int)) which means goes in the order of source(destination(value)). A subsequent process needs to check this container, to see if an element exists in for a

Vector Initialisation in C++

自闭症网瘾萝莉.ら 提交于 2019-12-02 04:38:45
I am using Vectors in my code. The line that is causing the error is as follows : vector<Node> alt_seq ; alt_seq = vector<Node>(1000); for(int j=0; j<alt_cf.getNoOfNodes(i); j++) { Node temp_node = *alt_itr; alt_itr++; alt_seq.push_back(temp_node); } The line : alt_seq.push_back(temp_node); causes a runtime error. However if I initialise the Vector with some initial size as follows: vector<Node> alt_seq(1000) ; In this case the code works fine. However I do not want to give an initial size as the number of objects in the vector will be variable at runtime. Please help me. I am new with C++.