Reputation: 1253
I wrote a simple program that accesses a file called "input.txt" and pushes its contents into a vector of the string type.
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
int main()
{
fstream input;
input.open("input.txt");
string s;
input >> s;
while (!input.eof())
{
cout << s <<endl;
input >> s;
vector<string> v;
v.push_back(s);
}
input.close();
}
I know this isn't the best way to do this but is there a way that I can then access this vector and use it for an equation? Say if I wanted to add all the elements of the vector together and find the sum?
Upvotes: 1
Views: 355
Reputation: 58715
Your program is incorrect. What you probably meant is this:
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
int main()
{
ifstream input("input.txt");
string s;
vector<string> v;
while (input >> s)
{
cout << s <<endl;
v.push_back(s);
}
}
Also, here's another way of doing the same thing:
#include<algorithm>
#include<iterator>
#include<fstream>
#include<string>
struct ShowAndCopy {
const std::string& operator()(const std::string& s) const {
std::cout << s;
return s;
}
};
int main()
{
std::ifstream input("input.txt");
std::vector<std::string> v;
std::transform(std::istream_iterator<std::string>(input),
std::istream_iterator<std::string>(),
std::back_inserter(v),
ShowAndCopy());
return 0;
}
Upvotes: 2
Reputation: 373112
For starters, here's a much cleaner version of the code you've just written:
ifstream input("input.txt");
string s;
while (input >> s) {
cout << s << endl;
vector<string> elems;
elems.push_back(s);
}
This uses ifstream
instead of fstream
, which seems appropriate here since you aren't using the write facilities of fstream
. It also combines the read logic with the loop condition, simplifying the logic for when to continue.
One thing that seems weird with this code is that you've scoped the vector
so that it only lives for one loop iteration. Consequently, every iteration, you lose all your old vector
contents. Moving this out of the loop would probably fix this:
ifstream input("input.txt");
vector<string> elems;
string s;
while (input >> s) {
cout << s << endl;
elems.push_back(s);
}
Finally, if you want to loop over the elements of the vector
adding them together, you probably don't want to be reading the file as strings, but rather as int
s or double
s. This saves you the hassle of having to convert the values later. For example:
ifstream input("input.txt");
vector<double> elems;
double s;
while (input >> s) {
cout << s << endl;
elems.push_back(s);
}
Now, you can add all the values together like this:
double total = 0.0;
for (int i = 0; i < elems.size(); ++i)
total += elems[i];
Or, even better, you can add everything together using the accumulate
algorithm from <numeric>
:
double total = accumulate(elems.begin(), elems.end(), 0.0);
Hope this helps!
Upvotes: 4
Reputation: 754860
Yes, you can iterate over the contents of the vector of strings, converting the values to an integer or floating point value and summing them.
Upvotes: 0