Solved

boost variant type and vector

Posted on 2014-01-23
9
695 Views
Last Modified: 2014-03-06
Hi

I declare a vector whose type is a boost::variant

typedef boost::variant<unsigned char,unsigned short> mytype;
std::vector<mytype> v;

When I try to do an insertion into my vector, I first check if it is empty.If so, I add the element and get a pointer to my first element. This works as expected.

if (v.empty()){
      v.push_back(mydata); //works
        begin=v.begin();
}else{

       v.insert(begin+offset,mydata); //does not work

}

But when I insert my second element, the code breaks.

Visual Studio breaks at the following code, saying ".exe has triggered a breakpoint"

_CRTIMP void _cdecl _CrtDbgBreak(
    void
    )
{
    DebugBreak();
}

If i trace back to my code which is giving rise to this problem, it is this line:
v.insert(begin+offset,mydata);

And this seems to have gotten stuck somewhere in vector.h
_Myt& operator+=(difference_type _Off)
            {      // increment by integer
            (*(_Mybase *)this) += _Off;
            return (*this);
}

Any ideas? I though vector could do an insert even if the type was variant. Is this not so?
0
Comment
Question by:LuckyLucks
9 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 39804748
>>  v.insert(begin+offset,mydata); //does not work

And if you try using push_back?

v.push_back(mydata);
0
 
LVL 40

Expert Comment

by:evilrix
ID: 39804770
What value does offset have? If must be >= 0 && <= begin() + size(), otherwise you are trying to insert into an invalid location as it doesn't exist. If you want the vector to grow when you insert past the end us back_inserter.

http://www.cplusplus.com/reference/iterator/back_inserter/
0
 
LVL 86

Expert Comment

by:jkr
ID: 39805165
While looking at

if (v.empty()){
      v.push_back(mydata); //works
        begin=v.begin();
}else{

       v.insert(begin+offset,mydata); //does not work

}

Open in new window


it seems that 'begin' is only initialized when 'v' is empty. If it is not emtpy, it seems to remain uninitialized. Did you mean to

if (v.empty()){
      v.push_back(mydata); //works
        begin=v.begin();
}else{

       v.insert(v.begin()+offset,mydata); 

}

Open in new window


?
0
 
LVL 33

Assisted Solution

by:sarabande
sarabande earned 350 total points
ID: 39805930
the vector cannot grow automatically. it would throw an exception if v.begin() + offset points to an address which is far beyond v.end() what is the address beyond the last element of the internal c array.

if you want the vector to grow automatically you could do:

if (offset < v.size()){
        mydata[offset] = mydata;;
}else{
       v.resize(offset); // creates empty slots
       v.push_back(mydata); 
}

Open in new window



Sara
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:LuckyLucks
ID: 39822355
if i have a rather large vector (16000000 elements)into which to insert at random places, it would take a long time if i resized. is there a better way?
0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 150 total points
ID: 39822664
>> if i have a rather large vector (16000000 elements)into which to insert at random places, it would take a long time if i resized. is there a better way?

Well, that is a different question and really should be opened as such; however...

If you know you have that many items to put into in the set the size first. If you don't know (and can't make a good initial guess) a std::list might be better but it's seek time is O(N). Alternatively, you are probably better off using a std::set or std::unordered_set but neither will preserve locality.

Even if you presize the vector, if your insertion "moves" things it'll be slow. In fact, it'll be O(N) time complexity.
0
 
LVL 33

Accepted Solution

by:
sarabande earned 350 total points
ID: 39823383
to add to above comment:

slowness may not be the only pitfall of such a size. the internal array needs to allocate a contiguous piece of heap memory what means in case of growing you need to have twice the space 16,000,000*sizeof(element).

a set as suggested by evilrix or a map would store the elements in a tree which does need more memory but not contiguous memory. additionally they provide a fast lookup.

Sara
0
 
LVL 33

Expert Comment

by:sarabande
ID: 39823388
if you would use a std::map<int, MyType> you could use it exactly like a vector but with binary search speed.

std::map<int, MyType> vmap;

....
vmap[12345] =MyType(...); // don't care for empty slots

Open in new window


Sara
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now