forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. ex8_02 should print, fixed. 2. added ex8_09 ex8_11 and ex8_13 cpp file. 3. move data file to `data` folder. 4. simplify @Mooophy's codes.
- Loading branch information
Showing
8 changed files
with
198 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// | ||
// ex8_09.cpp | ||
// Exercise 8.9 | ||
// | ||
// Created by pezy on 11/29/14. | ||
// Copyright (c) 2014 pezy. All rights reserved. | ||
// | ||
// @Brief Use the function you wrote for the first exercise in § 8.1.2 (p.314) to print the contents of an istringstream object. | ||
// @See Exercise 8.1 | ||
|
||
#include <iostream> | ||
#include <sstream> | ||
using std::istream; | ||
|
||
istream& func(istream &is) | ||
{ | ||
std::string buf; | ||
while (is >> buf) | ||
std::cout << buf << std::endl; | ||
is.clear(); | ||
return is; | ||
} | ||
|
||
int main() | ||
{ | ||
std::istringstream iss("hello"); | ||
func(iss); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,42 @@ | ||
//! @Alan | ||
//! | ||
//! Exercise 8.10: | ||
//! Write a program to store each line from a file in a vector<string>. | ||
//! Now use an istringstream to read each element from the vector a word at a time. | ||
//! | ||
//! | ||
|
||
// | ||
// ex8_10.cpp | ||
// Exercise 8.10 | ||
// | ||
// Created by pezy on 11/29/14. | ||
// Copyright (c) 2014 pezy. All rights reserved. | ||
// | ||
// @Brief Write a program to store each line from a file in a vector<string>. | ||
// Now use an istringstream to read each element from the vector a word at a time. | ||
|
||
#include <iostream> | ||
#include <string> | ||
#include <fstream> | ||
#include <sstream> | ||
#include <vector> | ||
#include <string> | ||
|
||
using std::vector; using std::string; using std::ifstream; using std::istringstream; using std::cout; using std::endl; using std::cerr; | ||
|
||
std::vector<std::string> &ex84(const std::string &fn, std::vector<std::string> &v); | ||
std::vector<std::string> &ex85(const std::string &fn, std::vector<std::string> &v); | ||
|
||
//! Exercise 8.10 | ||
int main() | ||
{ | ||
std::vector<std::string> v; | ||
v = ex84("test.txt", v); | ||
for (std::vector<std::string>::iterator it = v.begin(); it != v.end(); ++it) | ||
{ | ||
std::istringstream sfin(*it); | ||
std::string s; | ||
while(sfin>>s) | ||
{ | ||
std::cout<<s; | ||
} | ||
|
||
} | ||
return 0; | ||
} | ||
|
||
std::vector<std::string> &ex85(const std::string &fn, std::vector<std::string> &v) | ||
{ | ||
std::string word; | ||
std::ifstream fin(fn); | ||
|
||
if(fin.is_open()) | ||
{ | ||
std::cout<<"opened\n"; | ||
} | ||
else | ||
{ | ||
std::cout<<"failed\n"; | ||
} | ||
|
||
while(fin>>word) | ||
{ | ||
v.push_back(word); | ||
} | ||
|
||
return v; | ||
} | ||
|
||
|
||
std::vector<std::string> &ex84(const std::string &fn, std::vector<std::string> &v) | ||
{ | ||
std::string line; | ||
std::ifstream fin(fn); | ||
|
||
if(fin.is_open()) | ||
{ | ||
std::cout<<"opened\n"; | ||
} | ||
else | ||
ifstream ifs("../data/book.txt"); | ||
if (!ifs) | ||
{ | ||
std::cout<<"failed\n"; | ||
cerr << "No data?" << endl; | ||
return -1; | ||
} | ||
|
||
while(std::getline(fin,line)) | ||
|
||
vector<string> vecLine; | ||
string line; | ||
while (getline(ifs, line)) | ||
vecLine.push_back(line); | ||
|
||
for (auto &s : vecLine) | ||
{ | ||
v.push_back(line); | ||
istringstream iss(s); | ||
string word; | ||
while (iss >> word) | ||
cout << word << endl; | ||
} | ||
|
||
return v; | ||
return 0; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// | ||
// ex8_11.cpp | ||
// Exercise 8.11 | ||
// | ||
// Created by pezy on 11/29/14. | ||
// Copyright (c) 2014 pezy. All rights reserved. | ||
// | ||
// @Brief The program in this section defined its istringstream object inside the outer while loop. | ||
// What changes would you need to make if record were defined outside that loop? | ||
// Rewrite the program, moving the definition of record outside the while, and see whether you thought of all the changes that are needed. | ||
|
||
#include <iostream> | ||
#include <sstream> | ||
#include <string> | ||
#include <vector> | ||
using std::vector; using std::string; using std::cin; using std::istringstream; | ||
|
||
struct PersonInfo { | ||
string name; | ||
vector<string> phones; | ||
}; | ||
|
||
int main() | ||
{ | ||
string line, word; | ||
vector<PersonInfo> people; | ||
istringstream record; | ||
while (getline(cin, line)) | ||
{ | ||
PersonInfo info; | ||
record.clear(); | ||
record.str(line); | ||
record >> info.name; | ||
while (record >> word) | ||
info.phones.push_back(word); | ||
people.push_back(info); | ||
} | ||
|
||
for (auto &p : people) | ||
{ | ||
std::cout << p.name << " "; | ||
for (auto &s : p.phones) | ||
std::cout << s << " "; | ||
std::cout << std::endl; | ||
} | ||
|
||
return 0; | ||
} |
Oops, something went wrong.