944,185 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 737
  • C++ RSS
Nov 4th, 2009
0

Accessing vector elements

Expand Post »
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?
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
AdRock is offline Offline
64 posts
since Dec 2008
Nov 4th, 2009
-7
Re: Accessing vector elements
If you want the 2d vector to contains either strings or ints, then maybe you need to create a template class.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Nov 5th, 2009
0
Re: Accessing vector elements
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

C++ Syntax (Toggle Plain Text)
  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?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
AdRock is offline Offline
64 posts
since Dec 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Need help finding a C++ Profiler
Next Thread in C++ Forum Timeline: C++, how to read one character/encrypt?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC