Solved

boost variant type and vector

Posted on 2014-01-23
9
766 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

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 …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

630 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