Initializing multiple (unique) entries of a vector

vector c++
initialize vector after declaration c++
initialize vector c++
assign values to vector c++
multidimensional vector c++
null vector c++
initialize vector of vectors c++
populate a vector c++

With an array you can do this:

int a[] = { 1,2,3,4,5,6 }

How do you do this with a vector?

You could write this, though its not an initialization, but looks very appealing:

std::vector<int> v;
v << 1,2,3,4,5,6,7,8,9,10; //all pushed into the vector!

And to support this syntax, you need two utility operators defined as:

template<typename T>
std::vector<T>& operator << (std::vector<T>& v, const T & item)
{
    v.push_back(item);  return v;
}
template<typename T>
std::vector<T>& operator,(std::vector<T>& v, const T & item)
{
    v.push_back(item);  return v;
}

And you're done!

Test code:

int main() {
       std::vector<int> v;
       v << 1,2,3,4,5,6,7,8,9,10;

       for ( size_t i = 0 ; i < v.size() ; ++i )
             std::cout << v[i] << " ";
}

Output:

1 2 3 4 5 6 7 8 9 10 

Online demo : http://www.ideone.com/1hyR3

Using the utility operators, you actually can avoid temporary variables such as the one in @Als's solution, and you can write this without boost!

Initialize a vector in C++ (5 different ways), // and push values one by one. #include <bits/stdc++.h>. using namespace std;. Initialization vector can be done in many ways1) Initialize a vector by push_back() methodAlgorithmBegin Declare v of vector type. Call push_back() fu

If you are using Boost, You can simply use Boost.Assign

std::vector<int> v = boost::assign::list_of(1)(2)(3)(4)(5)(6);

Or

If you are not using Boost, you can do it in two steps:

static const int a[] = {1,2,3,4,5,6};
vector<int> v (a, a + sizeof(a) / sizeof(a[0]) );

std::unique in C++, Competitive Programming � Design Patterns � Multiple Choice std::unique is used to remove duplicates of any element present this vector, all the sub- groups having consecutive duplicate elements Declaring a string. The code creates a 2D vector by using the push_back() function and then displays the matrix. Syntax: vector_name.push_back(value) where value refers to the element to be added in the back of the vector Example 1: v2 = {1, 2, 3} v1.push_back(v2); This function pushes vector v2 into vector of vectors v1. Therefore v1 becomes { {1, 2, 3} }.

If you don't want to use boost, you can use this:

std::vector<int> myvec;
int myints[] = {1776, 7, 4};
myvec.assign(myints, myints + 3);

Not exactly the same, but it comes very close. I always use this.

C++ std::vector : declare, initialize, functions of vector, etc, In std::vector also, the size() function returns the length (i.e. number of elements in the vector). Let's see an example of std::vector. #include <iostream> #� Initializing all the weights with zeros leads the neurons to learn the same features during training. In fact, any constant initialization scheme will perform very poorly. Consider a neural network with two hidden units, and assume we initialize all the biases to 0 and the weights with some constant $\alpha$. If we forward propagate an input

As of C++ 11 you can create a vector object with list-initialization as follows:

std::vector<int> v1 = {1, 2, 3, 4};
std::vector<int> v2 {1, 2, 3, 4};

And in C++ 17 you can omit the type to allow C++ to infer the type based on the datatype passed in the list (provided you don't mix the types):

// C++ 17
std::vector v3 = {1, 2, 3, 4};
std::vector v4 {1, 2, 3, 4};

Multiple distinct objects, REM Declare an array of structures; all members are initialised to zero: DIM objects{(n%) a%, b$} REM Initialise the objects to distinct values: Scalar-vector addition I in Julia, a scalar and a vector can be added I the scalar is added to each entry of the vector [2, 4, 8] + 3 gives (in mathematical notation) 2 4 2 4 8 3 5+31 = 2 4 5 7 11 3 5 Vector operations 13

6.3 Vector, Matrix, and Array Expressions, This syntax provides a way declare and define small vectors a single line, as follows. The vector expression values may be compound expressions or variable� This should be used only when vector dimensions are not known in advance. 3. Fill Constructor. The recommended approach is to use fill constructor of the vector container for constructing a vector of vectors. The fill constructor accepts an initial size n and a value and creates a vector of n elements and fills with the specified default value.

unique: Extract Unique Elements, unique returns a vector, data frame or array like x but with duplicate elements/ rows removed. This will initialize the num array with value 1 at all index. We may also ignore the size of the array: int num[ ] = {1, 1, 1, 1, 1} The array will be initialized to 0 in case we provide empty initializer list or just specify 0 in the initializer list.

Vectors and Matrices — GSL 2.6 documentation, This function allocates memory for a block of n double-precision elements, returning a pointer to the block struct. The block is not initialized and so the values of its� Initializing an empty list turns out to have an added benefit over my rep(NA) method for vectors; namely, the list ends up actually empty, not filled with NA’s. Confusingly, the function to use is “vector,” not “list.”

Comments
  • You currently cannot use initializer lists with vectors, but that is a feature in C++0x, which to my knowledge has yet to be implemented in compilers.
  • possible duplicate of how-to initialize 'const std::vector<T>' like a c array
  • @Shaktal: It's certainly implemented in my (fairly old) version of GCC, as long as you enable it with --std=c++0x. But you're correct, it's not valid in C++03.
  • @Mike Ahh, I wasn't aware that it was implemented in GCC, although I do know that it's possibly the most up to date compiler out there (probably due to it's open-source nature). Thanks for clearing that up! :)
  • looks very nice, still it doesn't work with size_t or double (error starts with error: no match for ‘operator<<’ in ‘v << 1’ on g++ (Debian 4.7.2-5) 4.7.2)
  • @hardmooth: Use two different template parameters if you want to allow mismatched (but convertible) types.
  • yes, you're right. templating it to template<typename S, typename T> std::vector<S>& operator << (std::vector<S>& v, const T & item) does the trick. Thanks!
  • Are you missing a ";" on the second line in your first code block?