| | |
Creating two dimensional array
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
•
•
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..
c++ Syntax (Toggle Plain Text)
#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; }
•
•
Join Date: Aug 2007
Posts: 49
Reputation:
Solved Threads: 0
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;
}
#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;
}
•
•
•
•
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
Last edited by iamthwee; Aug 27th, 2007 at 6:37 am.
![]() |
Similar Threads
- Need Help With two-dimensional array (VB.NET)
- two dimensional array (C)
- Validation with js of two dimensional array (JavaScript / DHTML / AJAX)
- 2 dimensional array class (C)
- multi dimensional array search xml parser (PHP)
- Need help passing a multi-dimensional array (C++)
- Trying to create a method to convert string letters into a two dimensional array (Java)
Other Threads in the C++ Forum
- Previous Thread: hi,am a beginner in C++please tell me how I should use/implement the setpos function
- Next Thread: linked list
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library linker list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






