stdvector

Basic question about std::vector instantiation

This looks simple but I am confused: The way I create a vector of hundred, say, ints is std::vector<int> *pVect = new std::vector<int>(100); However, looking at std::vector's documentation I see that its constructor is of the form explicit vector ( size_type n, const T& value= T(), const Allocator& = Allocator() ); So, how does th...

Putting a C++ Vector as a Member in a Class that Uses a Memory Pool

Hey, I've been writing a multi-threaded DLL for database access using ADO/ODBC for use with a legacy application. I need to keep multiple database connections for each thread, so I've put the ADO objects for each connection in an object and thinking of keeping an array of them inside a custom threadInfo object. Obviously a vector would ...

returning a pointed to an object within a std::vector

I have a very basic question on returning a reference to an element of a vector . There is a vector vec that stores instances of class Foo. I want to access an element from this vector . ( don't want to use the vector index) . How should I code the method getFoo here? #include<vector> #include<stdio.h> #include<iostream> #include<ma...

returning reference to a vector from a method and using its public members

dear experts, I have a vector t_vec that stores references to instances of class Too. The code is shown below. In the main , I have a vector t_vec_2 which has the same memory address as B::t_vec. But when I try to access t_vec_2[0].val1 it gives error val1 not declared. Could you please point out what is wrong? Also, if you know of ...

vector related memory allocation question

hi all, I am encountering the following bug. I have a class Foo . Instances of this class are stored in a std::vector vec of class B. in class Foo, I am creating an instance of class A by allocating memory using new and deleting that object in ~Foo(). the code compiles, but I get a crash at the runtime. If I disable delete my_a fro...

deleting element objects of a std vector using erase : a) memory handling and b) better way?

hi, I have a vec_A that stores instances of class A as: vec_A.push_back(A()); I want to remove some elements in the vector at a later stage and have two questions: a) The element is deleted as: vec_A.erase(iterator) Is there any additional code I need to add to make sure that there is no memory leak? . b) Assume that condition if(...

C++ User-Defined Vector

How do you declare a vector in c++ while allowing user input to define the vector's name? Okay, after reviewing your responses, here is more detail; Here is the error message from VS08 C++ console application - Error 2 error C2664: 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::get(_Elem *,std::streamsize)' : c...

passing a class method as opposed to a function in std::sort

hi, Within a class, I am trying to sort a vector, by passing a method of the same class. But it gives errors at the time of compilation. Can anyone tell what the problem is? Thank you! it gives the following error: argument of type bool (Sorter::)(D&, D&)' does not matchbool (Sorter::*)(D&, D&)' I have also tried using sortBynumb...

Struct size containing vector<T> different sizes between DLL and EXE..

I have this situation where an EXE program imports a DLL for a single function call. It works by passing in a custom structure and returning a different custom structure. Up till now it's worked fine until I wanted one of the structs data members to be a vector < MyStruct > When I do a sizeof(vector< MyStruct >) in my program I get a si...

Linker error with Duplicated Symbols, SWIG and C++ Vectors

I came across this error trying to compile a shared object from 2 sets of objects. The first set contains one .os object compiled from one cpp file generated by SWIG. The second set is contains all of the .so files from the individual files that make up the interface to be wrapped. $g++ -shared *.os -o Mathlibmodule.so ld: duplicate...

Weird behavior of std::vector

I have a class like this: class OBJ{...}; class A { public: vector<OBJ> v; A(int SZ){v.clear(); v.reserve(SZ);} }; A *a = new A(123); OBJ something; a->v.push_back(something); This is a simplified version of my code. The problem is in debug mode it works perfect. But in release mode it crashes at "push_back" line. (with all...

A cross between std::multimap and std::vector?

I'm looking for a STL container that works like std::multimap, but has constant access time to random n-th element. I need this because I have such structure in memory that is std::multimap for many reasons, but items stored in it have to be presented to the user in a listbox. Since amount of data is huge, I'm using list box with virtual...

why there is no find for vector in C++

what's the alternative? Should I write by myself? ...

no match for operator= using a std::vector

I've got a class declared like this: class Level { private: std::vector<mapObject::MapObject> features; (...) }; and in one of its member functions I try to iterate through that vector like this: vector<mapObject::MapObject::iterator it; for(it=features.begin(); it<features.end(); it++) { /* loop code */ } This ...

'std::vector<T>::iterator it;' doesn't compile

I've got this function: template<typename T> void Inventory::insertItem(std::vector<T>& v, const T& x) { std::vector<T>::iterator it; // doesn't compile for(it=v.begin(); it<v.end(); ++it) { if(x <= *it) // if the insertee is alphabetically less than this index { ...

question about std::vector::end()

I recently finished fixing a bug in the following function, and the answer surprised me. I have the following function (written as it was before I found the bug): void Level::getItemsAt(vector<item::Item>& vect, const Point& pt) { vector<itemPtr>::iterator it; // itemPtr is a typedef for a std::tr1::shared_ptr<item::Ite...

referencing a vector::front works, but vector::begin doesn't

I have this bit of code: cerr << client->inventory.getMisc().front()->getName() << endl; vector<itemPtr>::iterator it; it = client->inventory.getMisc().begin(); cerr << (*it)->getName() << endl; Let me explain that a bit: client is a tr1::shared_ptr that points to an object that has a member named inventory that has a private vector...

Unsigned char std::vector to unsigned char[]?

Simple question, how does one create a function which takes an unsigned char std::vector and spits out an unsigned char[] with a length. Thanks! Ah, well it seems my problem was my knowledge of std::vector. I always believed that std::vector did not hold its values in linear fashion. That solves a lot of my problems. Thanks! ...

Two short questions about std::vector

When a vector is created it has a default allocation size (probably this is not the right term to use, maybe step size?). When the number of elements reaches this size, the vector is resized. Is this size compiler specific? Can I control it? Is this a good idea? Do repeated calls to vector::size() recount the number of elements (O(n) ca...

How to call constructor of objects contained in a std::vector?

When I create a std::vector of objects, the constructor of these objects is not always called. #include <iostream> #include <vector> using namespace std; struct C { int id; static int n; C() { id = n++; } // not called // C() { id = 3; } // ok, called }; int C::n = 0; int main() { vector<C> vc; vc.resize...