How do i create a two dimensional dynamic array of type string using vector.
Also let me know if how can i use "find" on this array..

Recommended Answers

All 4 Replies

How do i create a two dimensional dynamic array of type string using vector.
Also let me know if how can i use "find" on this array..

You could approach like this

#include <vector>

template <typename T>
class dynamic_array
{
public:
  dynamic_array(){};
  dynamic_array(int rows, int cols)
  {
    for(int i=0; i<rows; ++i)
    {
      data_.push_back(std::vector<T>(cols));
    }
  }
  
  // other ctors ....

  inline std::vector<T> & operator[](int i) { return data_[i]; }

  inline const std::vector<T> & operator[] (int i) const { return data_[i]; }

  // other accessors, like at() ...

  void resize(int rows, int cols)
  {
    data_.resize(rows);
    for(int i = 0; i < rows; ++i)
      data_[i].resize(cols);
  }

  // other member functions, like reserve()....

private:
  std::vector<std::vector<T> > data_;  
};


int main()
{
  dynamic_array<int> a(3, 3);
  a[1][1] = 2;
  int x = a[1][1];
  return 0;
}
Member Avatar for iamthwee

Try looking into linked lists, they would appear more useful regarding the problem you described in your previous thread.

This is my code on which i m getting few problem my goal is to read an input file and create a graph out of it... Here i have indicated where i m facing problem pls help... Rather than understanding the complete code kindly help at the points where i got stuck

#include<iostream>
#include<vector>
#include<pending/stringtok.hpp>
#include<fstream>
#include<string>
#include<algorithm>
#include<ctype.h>
#include<boost/multi_array.hpp>
using namespace std;

int main()
    {
                string line;
     string FileName="LL.csv";
     ifstream myfile;
     myfile.open("LL.csv");
     static vector<string> vertex;
     vector<string>edges;
     int i;
    if(!myfile)
        {
         cout<<"\nunable to open the file \n Either the file doesnot exist or bad format";
         exit(0);
        }

        else
        {
            i=0;
            while (! myfile.eof() )
                {
                  cout<<"\nFile contents is :-";
                  getline(myfile,line);
                  cout <<line;
                  if(line=="")
                     break;
                  if(i==0)
                  {
                    boost::stringtok(vertex,line,",");
                    vertex.erase(vertex.begin());
                     cout<<"\nvertex set :-";
       for(vector<string>::const_iterator itn=vertex.begin();itn<vertex.end();itn++)
    {
      cout<<(*itn);
    }

    }
    int size =vertex.size();
// Now here i need to create a 2-D array
              vector< <vector<string> > edge(size,size);
//In the above statement i m getting error i don't know the syntax to create a two dimensional array.


                if(i>0)
     {
       int j=0;  
                   vector<string>::iteator fir=edge.begin[0][j];
                //Here in the above statement what i want is to get the position of first element of each row. say [0][0] or [1][0] Each time i iterate here j get incremented each time... Kindly let me know how should i do this.. This value im using in the following for loop..

            for(index sec=0;sec<size;sec++)
     {
       boost::stringtok(edge[fir][sec],line,",");
      }
      bool p=true;
      for(vector<string>const_iterator iter=vertex.begin();iter!=vertex.end();iter++)
      {
                    if(edge[fir][0]==(*iter))
    {
                             cout<<"Vertex is present in the set");
          p=false;
                           break;
    }
     } 
          if(!p)
                               {
                                 int second=0;
                                 do
                                 {
                                   interator pos=find(edge[fir][second].begin(),A1[fir][second].end(),"1");
                 //In the above statement i m trying to find out if "1" is present in a particular row of a two dimensional array.. If yes it should return the position kindly let me know how to do this
                                   if(pos!=A1[fir][second].end)
         { 
         pair<>={edge[fir][second],vertex[pos]};
        //Here i m trying to make pair between
                                  between the vertex and the edge. How to do this

              }
                                   second++;
                                  }while(pos!=A1.end);

                               else
                              {
                               cout<<"Vertex is not present invalid file format";
                              }

                       fir++;

                       }

                i++;
                }


            myfile.close();
        }

        return 0;
    }
Member Avatar for iamthwee

This is my code on which i m getting few problem my goal is to read an input file and create a graph out of it

Explain this in more detail using plain english. That would help us help you better. At the moment your code is unreadable and uses third party libraries most ppl wouldn't bother to download.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.