944,098 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 11960
  • C++ RSS
Aug 27th, 2007
0

Creating two dimensional array

Expand 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..
Similar Threads
Reputation Points: 8
Solved Threads: 0
Light Poster
tonyaim83 is offline Offline
49 posts
since Aug 2007
Aug 27th, 2007
0

Re: Creating two dimensional array

Click to Expand / Collapse  Quote originally posted by tonyaim83 ...
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
c++ Syntax (Toggle Plain Text)
  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. }
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005
Aug 27th, 2007
0

Re: Creating two dimensional array

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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Aug 27th, 2007
0

Re: Creating two dimensional array

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;
}
Reputation Points: 8
Solved Threads: 0
Light Poster
tonyaim83 is offline Offline
49 posts
since Aug 2007
Aug 27th, 2007
0

Re: Creating two dimensional array

Quote ...
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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: hi,am a beginner in C++please tell me how I should use/implement the setpos function
Next Thread in C++ Forum Timeline: linked list





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC