简体   繁体   中英

How Do I Get a subvector from a vector?

How do i access 1,2,10 from the below vector?

D = [[1, 2, 10], [2, 3, 20], [2, 5, 25]]

int i=B[i][0]-1;
int j <= B[i][1]-1;

Was Solving a Problem, I Had to use them in a for loop to check for the answer. Can Anyone help me out

Are you looking for this?

#include <iostream>
#include <vector>

int main()
{
  std::vector<std::vector<int>> d = { {1, 2, 10}, {2, 3, 20}, {2, 5, 25} };

  auto subvector = d[0];

  for (auto& value : subvector)
  {
    std::cout << value << ' ';
  }
  std::cout << "\n";

  // or (poor solution)

  for (int i = 0; i < 3; i++)
  {
    std::cout << subvector[i] << ' ';
  }
  std::cout << "\n";

  // or even

  for (int i = 0; i < 3; i++)
  {
    std::cout << d[0][i] << ' ';
  }
  std::cout << "\n";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM