Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access to the index of a 2D vector by at() function?

Tags:

c++

c++11

vector

I am going to initialize a 2D vector in a member function, where the input arguments are planned to be fed into determined indexes within the function argument. Furthermore, I'm not gonna use [] operator to member access due to safety stuffs. How can I use at() function in order to access to the index of a 2D vector, as below?

vector<vector<double>> weight;

void Connection::setWeight(const double& value, const double& i, const double& j)
{
    // The other scheme except: weight[i][j] = value;
}
like image 214
User Avatar asked Sep 01 '14 19:09

User


People also ask

How do I pass a vector to a specific index?

The most idiomatic way is to make your function take iterators: template<typename It> It::value_type maxsubarray(It begin, It end) { ... } and then use it like this: std::vector<int> nums(...); auto max = maxsubarray(begin(nums) + 2, end(nums));

How do you access a specific element in a vector?

Element access: reference operator [g] – Returns a reference to the element at position 'g' in the vector. at(g) – Returns a reference to the element at position 'g' in the vector. front() – Returns a reference to the first element in the vector. back() – Returns a reference to the last element in the vector.

How do you access a vector index?

Access an element in vector using vector::at() reference at(size_type n); reference at(size_type n); It returns the reference of element at index n in vector. If index n is out of range i.e. greater then size of vector then it will throw out_of_range exception.

What is a 2D vector in Java?

In a 2D vector, every element is a vector. Another approach to access the vector elements: Like Java’s jagged arrays, each element of a 2D vector can contain a different number of values. Exercise Problem: Define the 2D vector with different sizes of columns. 2D vectors are often treated as a matrix with “rows” and “columns” inside it.

How do you find the index of a vector in C++?

How to find index of a given element in a Vector in C++. find (): Used to find the position of element in the vector. Subtract from the iterator returned from the find function, the base iterator of the vector . Finally return the index returned by the subtraction.

How to access each element of a vector in Java?

Each elements of a vector can be accessed by using its index. It works similar to array, i.e. the index of the first element is 0, index of the second element is 1 etc. By using at () method. Let’s check how to do that with example for both. It returns a reference to the element at position n in the vector.

What does each index of vector store?

Each index of vector stores a vector which can be traversed and accessed using iterators. It is similar to an Array of Vectors but with dynamic properties. vector<vector<int>> vec { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9, 4 } }; where vec is the vector of vectors with different number of elements in different rows


2 Answers

You can use at twice. Also note, if i and j are indexes, they really shouldn't be double.

void Connection::setWeight(const double value, const size_t i, const size_t j)
{
    weight.at(i).at(j) = value;
}
like image 52
Cory Kramer Avatar answered Oct 13 '22 12:10

Cory Kramer


Risking to be downvoted for premature optimization, and because it doesn't really answers the question asked, still I would like to remind about a serious problem in such approach.

Like any multidimensional array, vector of vector is not contiguous, which makes its performance characteristics terribly bad because of lacking of spatial locality and associated cache misses.

You can trivially solve the problem by wrapping a single-dimensional array with a convenient interface of two-dimensional array.

template <class T>
class MyArray2D
{
    std::vector<T> data;
    size_t sizeX, sizeY;
public:

const T& at(int x, int y) const { return data.at(y + x * sizeY); }

T& at(int x, int y) { return data.at(y + x * sizeY); }

// wrap other methods you need of std::vector here
};

Note that code above written on the fly and wasn't compiled. Still, I believe you've got the idea.

See also:

  • What is cache friendly code?.
  • Spatial Locality of Reference: an Efficient Access Pattern in any Cache System
like image 21
Ivan Aksamentov - Drop Avatar answered Oct 13 '22 12:10

Ivan Aksamentov - Drop