Creating two dimensional array

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2007
Posts: 49
Reputation: tonyaim83 is an unknown quantity at this point 
Solved Threads: 0
tonyaim83 tonyaim83 is offline Offline
Light Poster

Creating two dimensional array

 
0
  #1
Aug 27th, 2007
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..
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: Creating two dimensional array

 
0
  #2
Aug 27th, 2007
Originally Posted by tonyaim83 View Post
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
  1. #include <vector>
  2.  
  3. template <typename T>
  4. class dynamic_array
  5. {
  6. public:
  7. dynamic_array(){};
  8. dynamic_array(int rows, int cols)
  9. {
  10. for(int i=0; i<rows; ++i)
  11. {
  12. data_.push_back(std::vector<T>(cols));
  13. }
  14. }
  15.  
  16. // other ctors ....
  17.  
  18. inline std::vector<T> & operator[](int i) { return data_[i]; }
  19.  
  20. inline const std::vector<T> & operator[] (int i) const { return data_[i]; }
  21.  
  22. // other accessors, like at() ...
  23.  
  24. void resize(int rows, int cols)
  25. {
  26. data_.resize(rows);
  27. for(int i = 0; i < rows; ++i)
  28. data_[i].resize(cols);
  29. }
  30.  
  31. // other member functions, like reserve()....
  32.  
  33. private:
  34. std::vector<std::vector<T> > data_;
  35. };
  36.  
  37.  
  38. int main()
  39. {
  40. dynamic_array<int> a(3, 3);
  41. a[1][1] = 2;
  42. int x = a[1][1];
  43. return 0;
  44. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Creating two dimensional array

 
0
  #3
Aug 27th, 2007
Try looking into linked lists, they would appear more useful regarding the problem you described in your previous thread.
Last edited by iamthwee; Aug 27th, 2007 at 6:28 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 49
Reputation: tonyaim83 is an unknown quantity at this point 
Solved Threads: 0
tonyaim83 tonyaim83 is offline Offline
Light Poster

Re: Creating two dimensional array

 
0
  #4
Aug 27th, 2007
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;
}
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Creating two dimensional array

 
0
  #5
Aug 27th, 2007
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.
Last edited by iamthwee; Aug 27th, 2007 at 6:37 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC