vector

C++: Best algorithm to check if a vector is sorted

What would be the best way to check that a std::vector is sorted ? Is there something faster than a loop checking that v[i]<=v[i+1] ? Is it faster/cleaner with iterators ? Or is it actually better to just call sort every time (though the "v is already sorted" case is quite common) ? We can safely assume the vector only contains PODs, us...

Why does my add method overwrite the Vector?

I am adding objects into a java Vector using its add(Object) method. In my example, the first 5 objects are identical, followed by 2 instances different from the first five. For some reasons, as soon as I insert the first one that is different, it changes the entire vector to that value! 'values' is an iterator containing something like...

Does myVector.erase(myPtr) delete the object pointed by myPtr ?

If I have the following code Foo *f = new Foo(); vector<Foo*> vect; vect.push_back(f); // do stuff vect.erase(f); Did I create a memory leak ? I guess so, but the word erase gives the feeling that it is deleting it. Writing this, I am wondering if it is not a mistake to put a pointer in a STL vector. What do you think ? ...

Ways to hash a numeric vector?

Are there any known hash algorithms which input a vector of int's and output a single int that work similarly to an inner product? In other words, I am thinking about a hash algorithm that might look like this in C++: // For simplicity, I'm not worrying about overflow, and assuming |v| < 7. int HashVector(const vector<int>& v) { cons...

Fast Vector rendering library for C or Python

I'm looking for a library like Cairo, just far faster. It has to be a library that works with C or Python. It also would be nice if I could output to PNG, and SVG. I am looking at Qt's arthur, but that's C++, and I'm not a fan. Any suggestions? Edit: another precondition is that it has to run under Linux. ...

Vector or diagram drawing webservice

I have a webapp from which I'd like to insert diagrams and images quickly and easily. I expect there is, somewhere out there, a webservice which will take a URL with a parameter that describes the vector graphics or diagram and returns an image. No unlike what google charts does for graphing data. Any resources or ideas on this? If I...

Matlab multiplication of 2 vectors error

Hi, i have a small function with 2 vectors alpha = 1 1 1 1 1 1 1 1 1 f_uv = 193 193 194 192 193 193 190 189 191 and i get the error message: "??? Error using ==> mtimes Integers can only be combined with integers of the same class, or scalar doubles." when i try to do this: alphaf_uv = alp...

C++ extend a vector (with another vector)

I'm a C/Python programmer in C++ land, really working with the STL for the first time. In Python, extending a list with another list uses the list's extend method: >>> v = [1, 2, 3] >>> v_prime = [4, 5, 6] >>> v.extend(v_prime) >>> print v [1, 2, 3, 4, 5, 6] In C++, I'm currently using this algorithmic approach for vector extension: ...

resize a vector down.. c++

I have a vector with 1000 "nodes" if(count + 1 > m_listItems.capacity()) m_listItems.reserve(count + 100); The problem is I also clear it out when I'm about to refill it. m_listItems.clear(); The capacity doesn't change. I've used the resize(1); but that doesn't seem to alter the capacity. So how does one change the reserv...

Vectors in Java, how to return multiple vectors in an object...

I'm working on a java program, and I have several vectors defined and filled (from a file) inside a method. I need to return the contents of all the vectors from the method. I have heard you can put them all in one object to return them. Is that possible, and if so, how? If not, do you have any possible solutions for me? Thanks in a...

How do I examine the contents of an std::vector in gdb, using the icc compiler?

I want to examine the contents of a std::vector in gdb but I don't have access to _M_impl because I'm using icc, not gcc, how do I do it? Let's say it's a std::vector for the sake of simplicity. There is a very nice answer here but this doesn't work if I use icc, the error message is "There is no member or method named _M_impl". There a...

How to use std::sort with a vector of structures and compare function?

Thanks for a solution in C, now I would like to achieve this in C++ using std::sort and vector: typedef struct { double x; double y; double alfa; } pkt; vector< pkt > wektor; filled up using push_back(); compare function: int porownaj(const void *p_a, const void *p_b) { pkt *pkt_a = (pkt *) p_a; pkt *pkt_b = (pkt *) p_b; ...

Looking for C++ STL-like vector class but using stack storage

Before I write my own I will ask all y'all. I'm looking for a C++ class that is almost exactly like a STL vector but stores data into an array on the stack. Some kind of STL allocator class would work also, but I am trying to avoid any kind of heap, even static allocated per-thread heaps (although one of those is my second choice). Th...

Using an epsilon value to determine if a ball in a game is not moving?

I have balls bouncing around and each time they collide their speed vector is reduced by the Coefficient of Restitution. Right now my balls CoR for my balls is .80 . So after many bounces my balls have "stopped" rolling because their speed has becoming some ridiculously small number. In what stage is it appropriate to check if a spee...

What function in the std library is there to binary search a vector and find an element?

I've got a node struct struct Node{CString text, int id;}; in a sorted vector. I'm wondering if there's a function in algorithm that will do a binary search of the vector and find an element. ...

Populate a vector<int> from integers in a char *

char *values = " 3 1 4 15"; vector<int> array; I want to populate the array with the values, 3,1,4,15 Is there a slick way to do it with the stl copy algorithm? ...

crash on vector::push_back

I have a vector that contains POSITIONs. When I try to push_back or clear, the program crashes. The callstack shows _invalid_parameter_noinfo in one of the frames. I googled and found a solution(defining _HAS_ITERA.... and _SECURE_SCL to 0) , but was not effective. I'm using VS2008 with MFC feature pack installed on vista. Please help. ...

How do stl containers get deleted?

How does container object like vector in stl get destroyed even though they are created in heap? EDIT If the container holds pointers then how to destroy those pointer objects ...

Vector graphics in iText PDF

We use iText to generate PDFs from Java (based partly on recommendations on this site). However, embedding a copy of our logo in an image format like GIF results in it looking a bit strange as people zoom in and out. Ideally we'd like to embed the image in a vector format, such as EPS, SVG or just a PDF template. The website claims that...

Strange behavior of Java's TreeMap put() method

I have the following code, which splits up a Vector into a string vector (to use as a key) and an integer at the end (to use as value). payoffs.put(new Vector<String>(keyAndOutput.subList(0, keyAndOutput.size() - 1)), Integer.parseInt(keyAndOutput.lastElement())); The TreeMap in question is constructed using a Comparator with the foll...