У меня 2 вопроса:
- почему мне выдает ошибку substr: не является членом "std::vector<Book,std::allocator<_Ty>>" ?
- в предпоследней строчке мне выдает ошибку не найден оператор "=". Мне надо его перегружать или где-то ошибка есть?
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include "BoolShelf.h"
#include "Book.h"
#include <fstream>
using namespace std;
istream& operator >> (std::istream& is, vector<string> & v)
{
string temp;
for (size_t i = 0; i < v.size(); i++)
{
getline(is, temp);
if (!is) break;
v[i] = temp;
}
return is;
}
ostream& operator << (ostream& os, vector<string> & v)
{
for (auto i : v)
{
os << i;
os << '\n';
}
return os;
}
bool func(string& s)
{
if (s[0] == 'v')
return true;
else return false;
}
void BookShelf::SaveBook()
{
vector<string> v;
vector<string>::iterator it = v.begin();
while (it != v.end())
{
it = find_if(it, v.end(), func);
if (it != v.end())
{
cout << *it << "\n";
it++;
}
}
ofstream f1("book.txt");
if (!f1.is_open())
{
cout << "Ошибка открытия файла!" << endl;
}
else
{
cout << "Файл открыт!" << endl;
v.resize(4);
cin >> v;
for (it = v.begin(); it != v.end(); it++)
f1 << *it << "\n";
}
f1.close();
}
void BookShelf::LoadBook()
{
vector<string> v;
vector<string>::iterator it = v.begin();
while (it != v.end())
{
it = find_if(it, v.end(), func);
if (it != v.end())
{
cout << *it << "\n";
it++;
}
}
string tmp;
ifstream f2("book.txt");
if (f2.is_open())
{
while (!f2.end())
{
getline(f2, tmp);
Book b(tmp);
books.push_back(b);
}
}
f2.close();
for (it = v.begin(); it != v.end(); it++)
cout << *it << "\n";
}
int BookShelf::Selectbyname()
{
string name;
string word = books.substr(0, string::npos);
cout << "Введите слово, по которому будет идти поиск" << endl;
cin >> name;
word = find(books.begin(), books.end(), name);
return 0;
}
BookShelf
иBook
увидеть.