Okay, here's another one. Reading a text file and then writing out the words in reverse order. I'm having trouble printing out the words in reverse order. How do I read strings from a file in reverse order them in a vector? Maybe that'll help.
Anyway, this is what I have right now (I've included the Exercise Specs in comments at the top of the file, as always):
// chapter11ex13.cpp : Defines the entry point for the console application.
// Osman Zakir
// 3 / 9 / 2017
// Bjarne Stroustrup: Programming: Principles and Practice Using C++ 2nd Edition
// Chapter 11 Exercise 13
// Exercise Specifications:
/**
* Reverse the order of words (defined as whitespace-separated strings) in a
* file. For example, Norwegian Blue parrot becomes parrot Blue Norwegian .
* You are allowed to assume that all the strings from the file will fit into
* memory at once.
*/
#include "../../cust_std_lib_facilities.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
int main()
{
usingnamespace std;
cout << "Please enter name of input file: ";
string ifname;
cin >> ifname;
ifstream ifs{ ifname };
if (!ifs)
{
cout << "Error: cannot open input file " << ifname << '\n';
keep_window_open();
return 1;
}
string file_input;
vector<string> input_vector;
while (getline(ifs, file_input))
{
input_vector.push_back(file_input);
reverse(input_vector.begin(), input_vector.end());
}
for (auto i = input_vector.rbegin(); i != input_vector.rend(); ++i)
{
cout << *i;
}
cout << '\n';
keep_window_open();
}
Right now, the input file's contents are printed out as is.
you're reading the whole line but you need to read it in word by word so that you can reverse it later on. so stringstream the line, stream >> temp_string (which parses on whitespace), push_back(temp_string) into std::vector<std::string> and then apply the reverse iterators to print
@gunnerfunner: Great advice, thanks. I'll see if I can do that without asking for further help (I'll try to finish it myself, but if I can't, I'll try asking again).
So basically I need to define an istringstream and read words with it one by one in a loop, and then push them back into the vector?
@me555: I have an input file. It's a really short file, though:
The Norwegian Blue parrot is a parrot that, obviously, would be from Norway if it existed.
Edit: Okay, I did it without istrinstream since I didn't get how to do that while also actually reading from the input file I want to read from. This is my current code (and it works perfectly as far as I can see):
// chapter11ex13.cpp : Defines the entry point for the console application.
// Osman Zakir
// 3 / 9 / 2017
// Bjarne Stroustrup: Programming: Principles and Practice Using C++ 2nd Edition
// Chapter 11 Exercise 13
// Exercise Specifications:
/**
* Reverse the order of words (defined as whitespace-separated strings) in a
* file. For example, Norwegian Blue parrot becomes parrot Blue Norwegian .
* You are allowed to assume that all the strings from the file will fit into
* memory at once.
*/
#include "../../cust_std_lib_facilities.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
std::vector<std::string> fill_v(std::istream &is);
void print_strings_v(const std::vector<std::string> &str_v);
int main()
{
usingnamespace std;
cout << "Please enter name of input file: ";
string ifname;
cin >> ifname;
ifstream ifs{ ifname };
if (!ifs)
{
cout << "Error: cannot open input file " << ifname << '\n';
keep_window_open();
return 1;
}
vector<string> input_v = fill_v(ifs);
print_strings_v(input_v);
keep_window_open();
}
std::vector<std::string> fill_v(std::istream &is)
{
usingnamespace std;
string input;
vector<string> input_v;
while (is >> input)
{
input_v.push_back(input);
}
return input_v;
}
void print_strings_v(const std::vector<std::string> &str_v)
{
usingnamespace std;
for (auto i = str_v.rbegin(); i != str_v.rend(); ++i)
{
cout << *i << ' ';
}
cout << '\n';
}
Anyone have a long input file I can test this baby out on?
to avoid reallocation of the vector during read what's the best way to reserve vector memory at the outset, depending on the file size?
the example here http://www.cplusplus.com/reference/istream/istream/tellg/ suggests a char array depending on the file size but how'd that translate to std::vector<std::string> as we're using in this case?
Actually Dragon it might be better to use the reserve() method just to allocate memory to prevent future reallocations. Giving an initial size to the vector means any subsequent push_back or emplace_back would start from the end of the sized vector:
// chapter11ex13.cpp : Defines the entry point for the console application.
// Osman Zakir
// 3 / 9 / 2017
// Bjarne Stroustrup: Programming: Principles and Practice Using C++ 2nd Edition
// Chapter 11 Exercise 13
// Exercise Specifications:
/**
* Reverse the order of words (defined as whitespace-separated strings) in a
* file. For example, Norwegian Blue parrot becomes parrot Blue Norwegian .
* You are allowed to assume that all the strings from the file will fit into
* memory at once.
*/
#include "../../cust_std_lib_facilities.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
std::vector<std::string> fill_v(std::istream &is);
void print_strings_v(const std::vector<std::string> &str_v);
int main()
{
usingnamespace std;
cout << "Please enter name of input file: ";
string ifname;
cin >> ifname;
ifstream ifs{ ifname };
if (!ifs)
{
cout << "Error: cannot open input file " << ifname << '\n';
keep_window_open();
return 1;
}
ifs.seekg(0, ifs.end);
size_t length = ifs.tellg();
ifs.seekg(0, ifs.beg);
vector<string> input_v;
input_v.reserve(length);
input_v = fill_v(ifs);
print_strings_v(input_v);
keep_window_open();
}
std::vector<std::string> fill_v(std::istream &is)
{
usingnamespace std;
string input;
vector<string> input_v;
while (is >> input)
{
input_v.push_back(input);
}
return input_v;
}
void print_strings_v(const std::vector<std::string> &str_v)
{
usingnamespace std;
for (auto i = str_v.rbegin(); i != str_v.rend(); ++i)
{
cout << *i << ' ';
}
cout << '\n';
}
By the way, how much of a chance do you guys think there is for the Networking TS to be standardized? I really want it to happen, but I can't go and participate in committee meetings (I probably shouldn't even if I could, though, since I'm still learning).
I'll read up on those later. But for now I think I can mark this as "solved".
@gunnerfunner: You think you could come over to the thread I made for Chapter 12 Exercise 11 (it's the one that mentions equilateral triangles in the title)? I have a runtime error about two polygons lying in a straight line, and apparently my y-axis values are all outside the window. I don't know how to best points in a GUI, I guess.