Solved

boost variant type and vector

Posted on 2014-01-23
9
720 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 34

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
 

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 34

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 34

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Create string splitting user defined function in C++ 23 335
c++ syntax question 9 57
object oriented programming comparison 5 85
Coding for the first time 9 62
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

740 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