Solved

boost variant type and vector

Posted on 2014-01-23
9
735 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: 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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

734 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