Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <vector>
- #include <cstddef> // std::size_t
- /* finddelim.cpp */
- using std::cout; using std::cin; using std::cerr;
- using std::endl; using std::string; using std::vector;
- using std::ifstream;
- string readFileIntoString(const string path) {
- ifstream input_file(path);
- if (!input_file.is_open()) {
- cerr << "Could not open the file - '"
- << path << "'" << endl;
- exit(EXIT_FAILURE);
- }
- string f= string((std::istreambuf_iterator<char>(input_file)), std::istreambuf_iterator<char>());
- input_file.close();
- return f;
- }
- vector<string> findDigits(string data)
- {
- std::size_t pos;
- string delim="-";
- vector <string>numbers;
- while ((pos = data.find(delim)) != string::npos) {
- numbers.push_back(data.substr(0, pos));
- data.erase(0, pos + delim.length());
- }// get last numbera on tail if any
- if(data.size()>0)
- numbers.push_back(data);
- return numbers;
- }
- vector<string> removeEmpties(vector<string> v)
- {
- auto isEmptyOrBlank = [](const std::string &s) {
- return s.find_first_not_of(" \t") == std::string::npos;
- };
- v.erase(std::remove_if(v.begin(), v.end(),
- isEmptyOrBlank), v.end());
- return v;
- }
- int main (){
- /* test file
- --242----2-343---3-33---943-3--3-5-223--848344- */
- string filename="finddigits";
- string file_contents;
- file_contents = readFileIntoString(filename);
- cout << file_contents << endl;
- vector<string>num=
- removeEmpties(findDigits(readFileIntoString(filename)));
- for(const auto &n:num)
- cout<< n <<" ";
- cout<<endl;
- exit(EXIT_SUCCESS);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement