Questions tagged [vector]

A vector is a single-dimensional array: it contains components that can be accessed using an integral index. In some languages the size of a vector can grow or shrink as needed to accommodate adding and removing items after the Vector has been created. Use 'vector-graphics' for graphic display.

A vector is a single-dimensional : it contains components that can be accessed using an integral index. In some languages the size of a vector can grow or shrink as needed to accommodate adding and removing items after the vector has been created.

The applicable C++ Standard Library container class is called std::vector and emulates a C-style array. It has advantages over an array; such as the additional functionality of resizing itself when inserting or removing elements. As with the traditional array, the memory backing the std::vector is guaranteed to be contiguous, hence it is also often used to interoperate with older C-style APIs whilst maintaining the resources required.

Beware that std::vector<bool> does not have the std::vector interface.

Java also has the old Vector collection that, differently from ArrayList, is synchronized (so safer for multithreading), but slower.

However, languages like C, FORTRAN, etc., also often name the usual, fixed size arrays as "vectors".

The term "Vector" originated from using the array data structure to represent a geometric vector the starts at the coordinate origin (0,0,0) and points to the point, determined by the values, stored in this array (x,y,z).

This is distinct from:

Related tags:

35006 questions
969
votes
29 answers

Concatenating two std::vectors

How do I concatenate two std::vectors?
yigal
836
votes
4 answers

Appending a vector to a vector

Assuming I have 2 standard vectors: vector a; vector b; Let's also say the both have around 30 elements. How do I add the vector b to the end of vector a? The dirty way would be iterating through b and adding each element via…
sub
  • 8,403
  • 3
  • 16
  • 3
798
votes
19 answers

How to find out if an item is present in a std::vector?

All I want to do is to check whether an element exists in the vector or not, so I can deal with each case. if ( item_present ) do_this(); else do_that();
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
738
votes
5 answers

Why is Java Vector (and Stack) class considered obsolete or deprecated?

Why is Java Vector considered a legacy class, obsolete or deprecated? Isn't its use valid when working with concurrency? And if I don't want to manually synchronize objects and just want to use a thread-safe collection without needing to make fresh…
fjsj
  • 10,995
  • 11
  • 41
  • 57
705
votes
29 answers

What is the easiest way to initialize a std::vector with hardcoded elements?

I can create an array and initialize it like this: int a[] = {10, 20, 30}; How do I create a std::vector and initialize it similarly elegant? The best way I know is: std::vector
Agnel Kurian
  • 57,975
  • 43
  • 146
  • 217
704
votes
16 answers

How do I erase an element from std::vector<> by index?

I have a std::vector, and I want to delete the nth element. How do I do that? Example: std::vector vec; vec.push_back(6); vec.push_back(-17); vec.push_back(12); vec.erase(???);
dau_man
  • 7,059
  • 3
  • 17
  • 5
632
votes
8 answers

Test if a vector contains a given element

How to check if a vector contains a given value?
medriscoll
  • 26,995
  • 17
  • 40
  • 36
484
votes
20 answers

Counting the number of elements with the values of x in a vector

I have a vector of numbers: numbers <- c(4,23,4,23,5,43,54,56,657,67,67,435, 453,435,324,34,456,56,567,65,34,435) How can I have R count the number of times a value x appears in the vector?
RQuestions
  • 4,843
  • 3
  • 16
  • 4
463
votes
10 answers

How to convert vector to array

How do I convert a std::vector to a double array[]?
ganuke
  • 4,915
  • 5
  • 20
  • 12
445
votes
11 answers

Why can't I make a vector of references?

When I do this: std::vector hello; Everything works great. However, when I make it a vector of references instead: std::vector hello; I get horrible errors like error C2528: 'pointer' : pointer to reference is illegal I want to put…
Colen
  • 13,428
  • 21
  • 78
  • 107
411
votes
7 answers

What are the differences between ArrayList and Vector?

What are the differences between the two data structures ArrayList and Vector, and where should you use each of them?
KushalP
  • 10,976
  • 6
  • 34
  • 27
408
votes
33 answers

How do I print out the contents of a vector?

How do I print out the contents of a std::vector to the screen? A solution that implements the following operator<< would be nice as well: template std::ostream &…
forthewinwin
  • 4,455
  • 4
  • 19
  • 17
388
votes
16 answers

Best way to extract a subvector from a vector?

Suppose I have a std::vector (let's call it myVec) of size N. What's the simplest way to construct a new vector consisting of a copy of elements X through Y, where 0 <= X <= Y <= N-1? For example, myVec [100000] through myVec [100999] in a vector…
An̲̳̳drew
  • 13,375
  • 13
  • 47
  • 46
386
votes
11 answers

Sorting a vector in descending order

Should I use std::sort(numbers.begin(), numbers.end(), std::greater()); or std::sort(numbers.rbegin(), numbers.rend()); // note: reverse iterators to sort a vector in descending order? Are there any benefits or drawbacks with one approach…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
382
votes
6 answers

std::vector versus std::array in C++

What are the difference between a std::vector and an std::array in C++? When should one be preferred over another? What are the pros and cons of each? All my textbook does is list how they are the same.
Zud
  • 4,247
  • 4
  • 24
  • 25
1
2 3
99 100