Solved

boost variant type and vector

Posted on 2014-01-23
9
690 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 32

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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 32

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 32

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 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.

708 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

17 Experts available now in Live!

Get 1:1 Help Now