Strings into vetcor

Hello,

I'm trying to store strings into one vector, and I'm thinking in do in this form:

std::vector<string> hold;

std::string srctmp = ttmsg->getSource();// my function getsource get my strings and that strings are put in srctmp. This function getSouce will get a lot of different strings that will be caught to srctmp and them put in a vector.

hold.push_back(srctmp);

Is this the best form to insert string in a vector, our someone knows best ways?

Regards
CMarco
That looks ok to me.
Do you know the number of elements beforehand?
Use the result of the function anonymously, perhaps?
1
2
3
std::vector<std::string> hold ;
// ...
hold.push_back( ttmsg->getSource() ) ;



> Do you know the number of elements beforehand?

If yes, std::generate_n() in conjunction with std::bind() would be elegant.
Hello,

First thanks for the tips.

After my strings are inside my vector named hold, I print the content using (if it exist better ways to do this I will be glade to know):
// print out content:
vector<int>::iterator it;
for (it=hold.begin() ; it<hold.end() ; it++)
cout << hex << *it;


If I want to put my vector content in a file.txt is this the best way:
ofstream myfile("/home/data/file.txt");
int vsize = myvector.size();
for(int n; n<vsize; n++)
myfile << myvector[n] << '\n';

I'm with a problem because my file.txt is not being created when I run.
Advises?

Regards,
CMarco




Hello,

I'm receiving strings form time to time and when I receive that strings I call my function above, where I put my strings in to a vector called vect_hold. After that I try to put the vector content in a file.txt to see what I receive in a better way, and what happens is that inside the file.txt it his only one line with one string. The other 30 strings that it also have to store in the file do not appear there.

Can someone advise me


void Switch::forwardMessage(std::string src){

// Declaracao do vector
std::vector<std::string> vect_hold;
vect_hold.push_back( src );
std::ofstream myfile;

// print out content:
std::vector<std::string>::iterator it;
for (it = vect_hold.begin() ; it < vect_hold.end() ; it++){
myfile.open ("/home/data/file.txt");
myfile << *it << '\n';
myfile.close();
EV << " " << *it << "\n";
}

}

Thanks in advanced,
CMarco
Last edited on
Hi,

One tip? is because of being always opening and closing the file each time this function is called?

Regards,
CMarco
The default behaviour when you open a file is to just discard the old content. You can change the behaviour by passing additional flags but a better way is to not open and close the file inside the loop. Just open the file once before you start reading and close the file when you are done.
The previous version of the code should be fine. If the second one opens the file, there's no reason for the first one not to.

When you're using fstream, ifstream or ofstream, do not use open/close.

To summarise:
1
2
3
4
ofstream myfile("/home/data/file.txt");  // this is the correct way to open the file, see RAII
size_t vsize = myvector.size();  // STL sizes are size_t, not int
for (size_t n; n<vsize; ++n)  // try to get used to pre increment rather than post, ++n instead of n++
    myfile << myvector[n] << '\n';  // you can use std::endl instead of '\n' 


See: http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization
You write always one record in your file.

Inside your function a llocal vector is created that gets only one string.

1
2
 std::vector<std::string> vect_hold;
 vect_hold.push_back( src );


Then this string is written in newly opened file previous content of which is erased.

So what you do is what you get.:)
Hi,

Yes it gets only on string each time the function is called, but I call the function many times so each of the strings are kept inside of the same vector, vect_hold, so push_back will add a new element at the end of the vector, after its current last element, each time.

so this is ok:
std::vector<std::string> vect_hold;
vect_hold.push_back( src );

And doing this I can see the content of the vector and the content is ok and correct.
// Output do vector
std::vector<std::string>::iterator it;
for (it=hold.begin() ; it<hold.end() ; it++)
EV << *it << "\n";

The problem is the file that appears in the right directory but the file is always empty
ofstream myfile("/home/data/file.txt");
size_t vsize = hold.size();
for (size_t n; n<vsize; ++n)
myfile << hold[n] << '\n';

It seems that the cycle for is not executed.
Someone have an idea why?

Regards,
CMarco
Empty vector maybe?
You forgot to initialize n in the for loop.
Hi again,

thanks you all for the help it was really important for me.

It is all ok now, i do:

std::ofstream myfile; //it have to have std:: !!

myfile.open ("/home/data/file.txt"); // open file

size_t vsize = vect_hold.size();
for (size_t n=0; n<vsize; n++){ // initialize the n !!
myfile << vect_hold[n] << '\n';
EV << "vect: " << vect_hold[n] << "\n"; // to see output
}
myfile.close(); // in the end close file

Regards,
Marco
Topic archived. No new replies allowed.