Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

can't see vector problem [too many trees]

Posted on 2003-03-27
3
Medium Priority
?
259 Views
Last Modified: 2010-04-01
i have a text file search where i store the results in a vector which is
initialised in main 'vector<char *> results;'


     //print the string    
          memset (printOut,' ',5);
          cout << printOut << endl;    
                         
     //add string to results vector
          vector<char *>::iterator iter = results.end();
          results.insert(iter, printOut);

after the search is complete i try to save the results to a file from a different function.
i pass the vector by reference: 'void saveResults(vector<char *> &results)'

if (outfile.is_open())
          {
              outfile << "search results:" << "\n";
          vector<char *>::iterator iter = results.begin();
                         
               for (iter = results.begin(); iter < results.end(); iter++)
               {
               outfile << *iter << "\n";     //output each element of the vector
               }
          outfile.close();


the output is blank both onscreen and in the file apart from the carriage returns.
i have checked the contents of the vector immediately after assigning the strings and it returns the strings no problem.

?
0
Comment
Question by:antonumia
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 400 total points
ID: 8218945
Hmm, it should be

results.insert(iter, strdup (printOut));

or just

results.push_back ( strdup (printOut));

Otherwise (as you are using a vector of pointers), you will simply end up adding the same pointer to the list each time and as you are changing teh memory it points to...

BTW: Why not using a 'vector<string>'?
0
 
LVL 1

Author Comment

by:antonumia
ID: 8218971
great,
that works fine.
is a vector<string> better?
0
 
LVL 86

Expert Comment

by:jkr
ID: 8219002
>>is a vector<string> better?

Sure - if you 'strdup()' a char*, you have to deallocate the vector entries when you no longer need them. a vector of strings takes care of teh on its own:

string str = printOut;

results.push_back ( str);



0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question