I have a 2D vector of different values. It could be text or numbers.
I need to be able to see what is in the vector and to do something depending on what is in there.
How do i access each element of the vector and add to a temp string variable so i can make a comparison?
Here is my code so far
#include <fstream>
#include <iostream>
#include <vector>
#include <cstring>
#include <string>
#include <sstream>
#include <stdio.h>
using namespace std;
/* function to split token with punctuation into seperate tokens
* if no punctuation is found the whole string goes into a token
* function also splits double punctuation into tokens so )) would
* each have it's own token
*/
std::vector<std::string> SplitOnPunct(std::string const& str,std::string const& punct )
{
std::vector<std::string> vec;
if (str.length() == 0) return vec;
std::string::size_type pos, end;
for (pos = 0; pos != std::string::npos; pos = end)
{
end = str.find_first_of(punct, pos);
if (end == pos && ++end == str.size()) end = std::string::npos;
vec.push_back(str.substr(pos, end - pos));
}
return vec;
}
int main()
{
int i;
int a=0;
string line;
int b=0;
string Line="Line";
ifstream myFile("scan.cm");
if (! myFile)
{
cout << "Error opening output fle" << endl;
return -1;
}
vector < vector < string > > info;
vector < string > data;
while( getline( myFile, line ) )
{
vector < string > data;
string value;
istringstream iss(line);
std::ostringstream p;
//add a line number as the first token
b++;
p << "Line: " << b;
data.push_back(p.str());
while (iss >> value)
{
//check if the current line is a comment
if(line[0] =='/' && line[1] == '*')
continue;
else if(line[0] =='*')
continue;
else if(line[0] =='*' && line[1] == '/')
continue;
//check if the rest of the line is a comment
unsigned int pos = line.find("//", 0 );
if(pos !=string::npos)
continue;
//split and token with punctuation into further tokens
vector<string> vec = SplitOnPunct(value, "+-*/<=!>{()};,");
//add each token to the inner vector
for (std::vector<std::string>::size_type a = 0; a < vec.size(); ++a)
data.push_back(vec.at(a));
}
//add the tokens to the out vector
info.push_back(data);
}
for ( vector< vector< string > > :: size_type i = 0, size = info.size(); i < size; ++i)
{
for ( vector < string > :: size_type j = 0, length = info[i].size(); j < length; ++j)
{
//check to see if the inner vectors are empty
if(info[i].size() > 1)
//this is where I need to access the vector
cout << info[i][j] << endl;
}
cout << endl;
}
myFile.close();
return 0;
}