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;
}

Recommended Answers

All 2 Replies

If you want the 2d vector to contains either strings or ints, then maybe you need to create a template class.

That is way too complicated for what i need to do as I have almost finished.

I found this example elsewhere online in a vector example and it looks similar to what i would need

for(int i=0;i < str_Vector.size(); i++)
{
std::string strd = str_Vector.at(i);
cout<<strd.c_str()<<endl;
}

There is the loop which I already have, but how would I access that vector at the point? Would I access the vector at 'j' becuase I am in he inner loop or is there another way I would need to do it?

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.