Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to let client using [i][j] access two dimension array?

How to let client using [i][j] access two dimension array?

Scheduled Pinned Locked Moved C / C++ / MFC
questiongraphicsdata-structurestutorial
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fantasy1215
    wrote on last edited by
    #1

    I need to implement a class like a CMyResultSet class. The client code which call CMyResultSet would look like this:

    CMyResultSet res;
    GetResultSet(res);
    for (int i = 0; i < res.rows(); ++i)
    {
    string res1 = res[i][0].tostring();
    }

    I have no idea how to implement [][] to let the client code calling like that. How would the method's prototype look like? And How can I save the searched result set in two dimension array, using std::vector or what? Thanks in advance!

    M L L A 4 Replies Last reply
    0
    • F fantasy1215

      I need to implement a class like a CMyResultSet class. The client code which call CMyResultSet would look like this:

      CMyResultSet res;
      GetResultSet(res);
      for (int i = 0; i < res.rows(); ++i)
      {
      string res1 = res[i][0].tostring();
      }

      I have no idea how to implement [][] to let the client code calling like that. How would the method's prototype look like? And How can I save the searched result set in two dimension array, using std::vector or what? Thanks in advance!

      M Offline
      M Offline
      Mohibur Rashid
      wrote on last edited by
      #2

      I went through internet and the most common suggestion i got is to use (int,int) instead of searching for [int] [int]; follow the link for example http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.8

      1 Reply Last reply
      0
      • F fantasy1215

        I need to implement a class like a CMyResultSet class. The client code which call CMyResultSet would look like this:

        CMyResultSet res;
        GetResultSet(res);
        for (int i = 0; i < res.rows(); ++i)
        {
        string res1 = res[i][0].tostring();
        }

        I have no idea how to implement [][] to let the client code calling like that. How would the method's prototype look like? And How can I save the searched result set in two dimension array, using std::vector or what? Thanks in advance!

        L Offline
        L Offline
        Lakamraju Raghuram
        wrote on last edited by
        #3

        I have tried it in this way:

        #include

        #include
        #include
        using namespace std;

        class CMyResultSet
        {
        public:
        CMyResultSet(){}
        ~CMyResultSet(){}

        string GetResult(unsigned int i, unsigned int j)
        {
        	return m\_Data\[i\]\[j\];
        }
        
        void SetResult(string Result, unsigned int i)
        {
        	if( i >= m\_Data.size())
        	{
        		// New push to 1-dimension
        		vector vec;
        		vec.push\_back(Result);
        		m\_Data.push\_back(vec);
        	}
        	else
        	{
        		// Push to an existing 1-dimension's 2nd dimension
        		m\_Data\[i\].push\_back(Result);
        	}
        }
        

        private:

        vector< vector > m\_Data;
        

        };

        void main()
        {
        CMyResultSet objResultSet;

        objResultSet.SetResult("00",0);
        objResultSet.SetResult("01",0);
        
        objResultSet.SetResult("10",1);
        objResultSet.SetResult("11",1);
        
        objResultSet.SetResult("20",2);
        
        cout<<"Value at 00:"<
        
        1 Reply Last reply
        0
        • F fantasy1215

          I need to implement a class like a CMyResultSet class. The client code which call CMyResultSet would look like this:

          CMyResultSet res;
          GetResultSet(res);
          for (int i = 0; i < res.rows(); ++i)
          {
          string res1 = res[i][0].tostring();
          }

          I have no idea how to implement [][] to let the client code calling like that. How would the method's prototype look like? And How can I save the searched result set in two dimension array, using std::vector or what? Thanks in advance!

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Access it via a pointer pointer.

          ============================== Nothing to say.

          1 Reply Last reply
          0
          • F fantasy1215

            I need to implement a class like a CMyResultSet class. The client code which call CMyResultSet would look like this:

            CMyResultSet res;
            GetResultSet(res);
            for (int i = 0; i < res.rows(); ++i)
            {
            string res1 = res[i][0].tostring();
            }

            I have no idea how to implement [][] to let the client code calling like that. How would the method's prototype look like? And How can I save the searched result set in two dimension array, using std::vector or what? Thanks in advance!

            A Offline
            A Offline
            Aescleal
            wrote on last edited by
            #5

            Does the problem you're working on particularly need 2 dimensional addressing like that? Quite often you'll find that in a table each column has different semantics and each row represents another entity with the same type of attributes as the one immediately above and below. So instead of using a vector of vectors or (shudder) a raw 2D array look to see if what you've is actually a simple, 1D, array of structures. You'll find that's particularly true if you're using a database. e.g. if instead of having:

            string table[5][1024];

            you might find something like this represents what your app is up to a bit more.

            struct thingy
            {
            std::string name_;
            std::string favourite_blanchmange_manufacturer_;
            std::string why_they_want_world_peace_;
            std::string smurf_theyd_most_like_to_stangle_;
            std::string this_will_never_be_used_youll_just_have_to_initialise_it_forever_;
            };

            thingy thingies[ 1024 ];

            If you still need a 2D array come back and I'll try and add something else in here about it (but it won't be for a while). Cheers, Ash

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups