Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6899337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:24:34+00:00 2026-05-27T07:24:34+00:00

I have defined two dimensional array using following definition typedef std::vector<std::vector<short> > table_t; Can

  • 0

I have defined two dimensional array using following definition
typedef std::vector<std::vector<short> > table_t;

Can I use std::for_each for this array, I want to pass row and col as a parameter to the function

Or is there a way to identify row and col in the function

following is the code to get more idea.

#include <vector>
#include <iostream>
#include <algorithm>

typedef std::vector<std::vector<short> > table_t;

void myfunction (int i) {
    std::cout << " " << i;
}
int main ( int argc , char **argv) {
    table_t t = table_t(5, std::vector<short>(5));
    int counter = 0;
    for (size_t row = 0; row < 5; ++row)
           for (size_t col = 0; col < 5; ++col)
               t[row][col] = counter++;

    std::for_each( t.begin(), t.end(), myfunction);
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:24:34+00:00Added an answer on May 27, 2026 at 7:24 am

    I think the solution is custom function object. Try something like this:

    struct process_col
    {
          int row_index;
          int col_index;
          process_col(int r) : row_index(r), col_index(0){}
          void operator()(short & data)
          {
              //use row_index, col_index, and data at this indices
    
               std::cout << "Value at (" <<row_index <<"," << col_index << ") is " << data << std::endl;
    
               col_index++; //at the bottom
          }
    };
    
    struct process_row
    {
          int row_index;
          process_row() : row_index(0){}
          void operator()(std::vector<short> & row)
          {
             std::for_each(row.begin(), row.end(), process_col(row_index));
             row_index++;
          }
    };
    

    And then use it as:

    std::for_each( t.begin(), t.end(), process_row());
    

    Online Demo : http://ideone.com/Dft8X

    Output:

    Value at (0,0) is 0
    Value at (0,1) is 1
    Value at (0,2) is 2
    Value at (0,3) is 3
    Value at (0,4) is 4
    Value at (1,0) is 5
    Value at (1,1) is 6
    Value at (1,2) is 7
    Value at (1,3) is 8
    Value at (1,4) is 9
    Value at (2,0) is 10
    Value at (2,1) is 11
    Value at (2,2) is 12
    Value at (2,3) is 13
    Value at (2,4) is 14
    Value at (3,0) is 15
    Value at (3,1) is 16
    Value at (3,2) is 17
    Value at (3,3) is 18
    Value at (3,4) is 19
    Value at (4,0) is 20
    Value at (4,1) is 21
    Value at (4,2) is 22
    Value at (4,3) is 23
    Value at (4,4) is 24
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to have static and constant two dimensional array inside a class. The
Today I incidentally defined a two dimensional array with the size of one dimension
I have defined the following two scopes and both working fine: scope :regular_friends, lambda
I have a header file for a game that declares a two-dimensional array for
I have defined two endpoints in my App.Config file as <system.serviceModel> <services> <service name=HostDirectAddress.ITestService
if I have two classes, and have defined an explicit type conversion between them,
I'm trying to draw two sprites on the same screen. I have defined the
Preface: I have two entities defined as a one-to-many relationship: A <<-------> B. B's
Assuming you have two lists defined in an xml schema, call them A and
I have a class with two methods defined in it. public class Routines {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.