Accessing vector elements

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2008
Posts: 57
Reputation: AdRock is an unknown quantity at this point 
Solved Threads: 0
AdRock AdRock is offline Offline
Junior Poster in Training

Accessing vector elements

 
0
  #1
30 Days Ago
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?
  1. Here is my code so far
  2. #include <fstream>
  3. #include <iostream>
  4. #include <vector>
  5. #include <cstring>
  6. #include <string>
  7. #include <sstream>
  8. #include <stdio.h>
  9.  
  10. using namespace std;
  11.  
  12. /* function to split token with punctuation into seperate tokens
  13.  * if no punctuation is found the whole string goes into a token
  14.  * function also splits double punctuation into tokens so )) would
  15.  * each have it's own token
  16.  */
  17. std::vector<std::string> SplitOnPunct(std::string const& str,std::string const& punct )
  18. {
  19. std::vector<std::string> vec;
  20.  
  21. if (str.length() == 0) return vec;
  22.  
  23. std::string::size_type pos, end;
  24.  
  25. for (pos = 0; pos != std::string::npos; pos = end)
  26. {
  27. end = str.find_first_of(punct, pos);
  28.  
  29. if (end == pos && ++end == str.size()) end = std::string::npos;
  30.  
  31. vec.push_back(str.substr(pos, end - pos));
  32. }
  33.  
  34. return vec;
  35. }
  36.  
  37. int main()
  38. {
  39. int i;
  40. int a=0;
  41. string line;
  42. int b=0;
  43. string Line="Line";
  44.  
  45. ifstream myFile("scan.cm");
  46.  
  47. if (! myFile)
  48. {
  49. cout << "Error opening output fle" << endl;
  50. return -1;
  51. }
  52.  
  53. vector < vector < string > > info;
  54. vector < string > data;
  55.  
  56. while( getline( myFile, line ) )
  57. {
  58. vector < string > data;
  59. string value;
  60. istringstream iss(line);
  61. std::ostringstream p;
  62.  
  63.  
  64. //add a line number as the first token
  65. b++;
  66. p << "Line: " << b;
  67. data.push_back(p.str());
  68.  
  69. while (iss >> value)
  70. {
  71. //check if the current line is a comment
  72. if(line[0] =='/' && line[1] == '*')
  73. continue;
  74. else if(line[0] =='*')
  75. continue;
  76. else if(line[0] =='*' && line[1] == '/')
  77. continue;
  78.  
  79. //check if the rest of the line is a comment
  80. unsigned int pos = line.find("//", 0 );
  81. if(pos !=string::npos)
  82. continue;
  83.  
  84. //split and token with punctuation into further tokens
  85. vector<string> vec = SplitOnPunct(value, "+-*/<=!>{()};,");
  86.  
  87. //add each token to the inner vector
  88. for (std::vector<std::string>::size_type a = 0; a < vec.size(); ++a)
  89. data.push_back(vec.at(a));
  90. }
  91. //add the tokens to the out vector
  92. info.push_back(data);
  93. }
  94.  
  95. for ( vector< vector< string > > :: size_type i = 0, size = info.size(); i < size; ++i)
  96. {
  97. for ( vector < string > :: size_type j = 0, length = info[i].size(); j < length; ++j)
  98. {
  99. //check to see if the inner vectors are empty
  100. if(info[i].size() > 1)
  101.  
  102. //this is where I need to access the vector
  103.  
  104. cout << info[i][j] << endl;
  105. }
  106. cout << endl;
  107. }
  108. myFile.close();
  109.  
  110. return 0;
  111. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,442
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning
 
-7
  #2
30 Days Ago
If you want the 2d vector to contains either strings or ints, then maybe you need to create a template class.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 57
Reputation: AdRock is an unknown quantity at this point 
Solved Threads: 0
AdRock AdRock is offline Offline
Junior Poster in Training
 
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

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

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?
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC