-3

I'm trying to assign a value to a vector, but I get this error. What is the issue about?

std::vector<bool> *boolVec = new std::vector<bool>(max, true);
boolVec[0] = false;

This code works:

boolVec->at(0) = false;
Dvole
  • 5,483
  • 8
  • 50
  • 86

3 Answers3

4

try:

(*boolVec)[0] = false;

You have a pointer to a vector, so you have to dereference the pointer first.

Adam
  • 15,548
  • 4
  • 47
  • 89
3

You have a pointer to a vector std::vector<bool> *boolVec This is not what you want. Just use a vector:

std::vector<bool> boolVec(max, true);
boolVec[0] = false;

The vector data will be stored on the heap.

If you truely need the pointer then you need to dereference it. You can use

(*boolVec)[0] = false;

or

 boolVec->at(0) = false;

Note that using ->at( will perform boundary checks and may be slower.

Fantastic Mr Fox
  • 27,453
  • 22
  • 81
  • 151
  • This initializes vector on stack, I may have very large vector size, thus I want to create it on heap. I also don't want to just push the value to vector, but set a specific value at index. – Dvole Nov 24 '15 at 21:29
  • 6
    @Dvole: The part that holds the bools goes on the heap either way. – user2357112 supports Monica Nov 24 '15 at 21:31
  • @Dvole The heavy part will be on the heap. This is absolutley what you want, dont make a pointer to a vector. – Fantastic Mr Fox Nov 24 '15 at 21:31
  • @Ben Thats something I always wanted to know but never dared to ask ;). – 463035818_is_not_a_number Nov 24 '15 at 21:32
  • @user2357112 thanks for clearing this up. – Dvole Nov 24 '15 at 21:33
  • There certainly are reasons to have a pointer to a vector, but the stated one is not one of them. – Benjamin Lindley Nov 24 '15 at 21:33
  • @BenjaminLindley Id love an example of a reason. Only thing i can think of is passing over physical interfaces or something crazy like that. Can you give me an example? – Fantastic Mr Fox Nov 24 '15 at 21:35
  • @Ben: Same reason you'd want a pointer to anything else. As a non-owning reassignable/nullable reference. – Benjamin Lindley Nov 24 '15 at 21:36
  • @BenjaminLindley, while I totally agree one might need a pointer to vector, I really have hard time trying to come with a valid scenario of dynamically creating a vector object. Just see no reason to. – SergeyA Nov 24 '15 at 22:09
  • @SergeyA: I'm only talking about the pointer part, not the dynamic allocating part. Although I can certainly also think of scenarios where I would want to dynamically allocate a vector, it would never be of the form `vector<...>* vp = new vector<...>;`. It might come in the form of `std::vector> vv;`. I would, in fact, never use `new` for anything (other than placement new), even when I want dynamically allocated raw memory. – Benjamin Lindley Nov 24 '15 at 22:19
  • @BenjaminLindley, interesting approach. How would you design a container of polymorphic types? – SergeyA Nov 24 '15 at 22:29
  • @SergeyA: Okay, you got me. I was using hyperbole when I said "never". I'll use it when it is the most convenient interface (for your example, it would be a container of `unique_ptr`, which would result in using `new` before `std::make_unique` existed). But if `unique_ptr` didn't already exist, I would not implement my version of it using allocating new. It would just use `malloc` with placement new. – Benjamin Lindley Nov 24 '15 at 22:40
1

There are several ways to use the subscript operator

For example

( *boolVec )[0] = false;

or

boolVec[0][0] = false;

or

boolVec->operator []( 0 ) = false;

Here is a demonstrative program

#include <iostream>
#include <iomanip>
#include <vector>


int main( void )
{
    std::vector<bool> *boolVec = new std::vector<bool>( 6, true );

    boolVec[0][0] = false;
    ( *boolVec )[2] = false;
    boolVec->operator []( 4 ) = false;

    for ( bool b : *boolVec ) std::cout << std::boolalpha << b << ' ';
    std::cout << std::endl;

    delete boolVec;
}


false true false true false true 

Consider also a possibility to define tha vector like

std::vector<bool> boolVec( 6, true );
Vlad from Moscow
  • 224,104
  • 15
  • 141
  • 268