| | |
Accessing vector elements
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Dec 2008
Posts: 57
Reputation:
Solved Threads: 0
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?
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?
C++ Syntax (Toggle Plain Text)
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; }
•
•
Join Date: Dec 2008
Posts: 57
Reputation:
Solved Threads: 0
0
#3 29 Days Ago
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
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?
I found this example elsewhere online in a vector example and it looks similar to what i would need
C++ Syntax (Toggle Plain Text)
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?
![]() |
Similar Threads
- How to update a vector? ie delete elements after accessing (C++)
- help with - Read csv into vector and display elements of vector. (C++)
- C and C++ Timesaving Tips (C++)
- reading in from vector to a two dimensional array (C)
- find the vector with the most elements (C++)
Other Threads in the C++ Forum
- Previous Thread: Need help finding a C++ Profiler
- Next Thread: C++, how to read one character/encrypt?
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






