stdvector

OpenGL: Using VBO with std::vector

我只是一个虾纸丫 提交于 2019-11-30 16:21:21
I'm trying to load an object and use VBO and glDrawArrays() to render it. The problem is that a simple float pointer like float f[]={...} does not work in my case, because I passed the limit of values that this pointer can store. So my solution was to use a vector. And it's not working... Here is my code: unsigned int vbo; vector<float*> vert; ... vert.push_back(new float(i*size)); vert.push_back(new float(height*h)); vert.push_back(new float(j*size)); ... glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(vert), &vert, GL_STATIC_DRAW); glBindBuffer

keep std vector/list sorted while insert, or sort all

梦想与她 提交于 2019-11-30 13:26:54
Lets say I have 30000 objects in my vector/list. Which I add one by one. I need them sorted. Is it faster to sort all at once (like std::sort), or keep vector/list sorted while I add object one by one? vector/list WILL NOT be modified later. When you are keeping your vector list sorted while inserting elements one by one , you are basically performing an insertion sort, that theoretically runs O(n^2) in worst case. The average case is also quadratic, which makes insertion sort impractical for sorting large arrays. With your input of ~30000 , it will be better to take all inputs and then sort

Pass, return and convert to vectors list of lists over JNI

╄→гoц情女王★ 提交于 2019-11-30 12:44:47
问题 I need to pass from Java List< List<MyPoint> > points; over jni to C++ and convert to std::vector< std::vector<MyPoint> > Process this vectors and return List< List<MyPoint> > How correct pass and return list of lists? How convert list of lists of objects in vector of vectors of objects and backward? 回答1: I solved this problem with standard tools. Create in Java class as objects (O) container (C) Pass array of objects (O) from Java code to native part Create from array vector in C++ code

vector vs map performance confusion

走远了吗. 提交于 2019-11-30 08:02:05
edit: I am specifically comparing std::vector 's linear search operations to the std::map binary search operations because that is what Herb's claim seemed to relate to. I know that using a binary search will move the performance from O(N) to O(log N) but that would not be testing Herb's claim Bjarne Stroustrup and Herb Sutter have both recently talked about how awesome std::vector is in situations one would expect std::list to be used, owing to the cost of cache misses during linked list traversal. (see http://channel9.msdn.com/Events/Build/2014/2-661 at the 48 minute mark) Herb made a

Vector of vectors, reserve

强颜欢笑 提交于 2019-11-30 07:58:00
问题 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

Can i push an array of int to a C++ vector?

99封情书 提交于 2019-11-30 07:08:40
问题 Is there any problem with my code ? std::vector<int[2]> weights; int weight[2] = {1,2}; weights.push_back(weight); It can't be compiled, please help to explain why: no matching function for call to ‘std::vector<int [2], std::allocator<int [2]> >::push_back(int*&)’ 回答1: The reason arrays cannot be used in STL containers is because it requires the type to be copy constructible and assignable (also move constructible in c++11). For example, you cannot do the following with arrays: int a[10]; int

Initializing std::vector with iterative function calls

我怕爱的太早我们不能终老 提交于 2019-11-30 04:40:21
In many languages, there are generators that help to initialize collections. In C++, if one wants to initialize a vector uniformly, one can write: std::vector<int> vec(10, 42); // get 10 elements, each equals 42 What if one wants to generate different values on the fly? For example, initialize it with 10 random values, or consecutive numbers from 0 to 9? This syntax would be convenient, but it does not work in C++11: int cnt = 0; std::vector<int> vec(10, [&cnt]()->int { return cnt++;}); Is there a nice way to initialize a collection by iterative function calls? I currently use this ugly

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

…衆ロ難τιáo~ 提交于 2019-11-30 04:07:50
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", file); return(1); } mxArray *pdata = matGetVariable(pmat, "LocalDouble"); // pdata -> pdata_v mxDestroy

push_back or emplace_back with std::make_unique

家住魔仙堡 提交于 2019-11-30 01:23:04
问题 Based on the answers in these questions here, I know that it is certainly preferred to use c++14's std::make_unique than to emplace_back(new X) directly. That said, is it preferred to call my_vector.push_back(std::make_unique<Foo>("constructor", "args")); or my_vector.emplace_back(std::make_unique<Foo>("constructor", "args")); That is, should I use push_back or emplace_back when adding an std::unique_ptr constructed from std::make_unique ? ==== EDIT ==== and why? c: <-- (tiny smile) 回答1: It

Remove first N elements from a std::vector

谁说我不能喝 提交于 2019-11-30 00:06:21
I can't seem to think of a reliable way (that also compacts memory) to remove the first N elements from a std::vector . How would one go about doing that? Mark Ransom Since you mention that you want to compact memory, it would be best to copy everything to a new vector and use the swap idiom. std::vector<decltype(myvector)::value_type>(myvector.begin()+N, myvector.end()).swap(myvector); Use the .erase() method: // Remove the first N elements, and shift everything else down by N indices myvec.erase(myvec.begin(), myvec.begin() + N); This will require copying all of the elements from indices N+1