#include
#include <string>
#include
#include
#include
using namespace std;
/*
*第一个版本 录入一篇文章 查找字符串
*
*/
ifstream &open_file(ifstream &is, const string &filename){
is.close();
is.clear();
is.open(filename.c_str());
return is;
}
void read_file(vector<string> &vec, string &filename){
ifstream is;
string word;
if(!open_file(is, filename)){
throw std::runtime_error("open_file_error");
}
while(is >> word){
vec.push_back(word);
}
is.close();
is.clear();
}
vector<string> find_str(vector<string>&vec, const string &str){
vector<string> res;
for(vector<string>::iterator it = vec.begin(); it != vec.end(); ++it){
string::size_type loc = it->find(str);
if(loc != string::npos)
res.push_back(*it);
}
return res;
}
void print(vector<string> &res){
for(vector<string>::iterator it = res.begin(); it != res.end(); ++it){
cout <<*it << endl;
}
}
int main(int argc, const char *argv[])
{
string filename = "a.txt";
vector<string> vec, res;
string str;
read_file(vec, filename);
while(cin >> str){
res = find_str(vec, str);
print(res);
}
return 0;
}
#include
#include <string>
#include
#include
#include
using namespace std;
/*
*第二个版本 用结构体 录入一篇文章 查找字符串
*/
ifstream &open_file(ifstream &is, const string &filename){
is.close();
is.clear();
is.open(filename.c_str());
return is;
}
struct Find{
vector<string> vec;
void read_file(string &filename){
ifstream is;
string word;
if(!open_file(is, filename)){
throw std::runtime_error("open_file_error");
}
while(is >> word){
vec.push_back(word);
}
is.close();
is.clear();
}
vector<string> find_str(const string &str){
vector<string> res;
for(vector<string>::iterator it = vec.begin(); it != vec.end(); ++it){
string::size_type loc = it->find(str);
if(loc != string::npos)
res.push_back(*it);
}
return res;
}
void print(vector<string> &res){
for(vector<string>::iterator it = res.begin(); it != res.end(); ++it){
cout <<*it << endl;
}
}
};
int main(int argc, const char *argv[])
{
string filename = "a.txt";
Find s;
s.read_file(filename);
string str;
vector<string> res;
while(cin >> str){
res = s.find_str(str);
s.print(res);
}
return 0;
}
#include
#include <string>
#include
#include
#include
using namespace std;
class Findstr{
public:
void read_file(const string &filename);
vector<string> find_string(const string &str);
private:
vector<string> words_;
};
void Findstr::read_file(const string &filename){
ifstream is;
is.close();
is.clear();
is.open(filename.c_str());
if(!is){
throw std::runtime_error("open file failed");
}
string str;
while( is >> str){
words_.push_back(str);
}
is.close();
}
vector<string> Findstr::find_string(const string &str){
vector<string> result;
for(vector<string>::iterator it = words_.begin(); it != words_.end(); ++it){
string::size_type loc = it->find(str);
if(loc != string::npos){
result.push_back(*it);
}
}
return result;
}
int main(int argc, const char *argv[])
{
Findstr s;
string filename = "a.txt";
s.read_file(filename);
string str;
while(cin >> str){
vector<string> res = s.find_string(str);
for(vector<string>::iterator it = res.begin(); it != res.end(); ++it){
cout <<*it << endl;
}
}
return 0;
}
#include
#include <string>
#include
using namespace std;
class Student{
public:
void set_name(const string &name){
name_ = name;
}
const string &get_name()const{
return name_;
}
void set_age(int age){
age_ = age;
}
int get_age() const{
return age_;
}
void set_score(int score){
score_ = score;
}
int get_socre() const{
return score_;
}
void print(ostream &os){
os <" " <" " < endl;
}
private:
string name_;
int age_;
int score_;
};
int main(int argc, const char *argv[])
{
Student s1;
s1.set_name("zhangsan");
s1.set_age(12);
s1.set_score(90);
s1.print(cout);
return 0;
}
#include
#include <string>
#include
using namespace std;
class Test{
public:
void set(int a, int b){
/*
*这里若用cout <*/
this->a = a;
this->b = b;
}
void print(){
cout <" " < endl;
}
private:
int a;
int b;
};
int main(int argc, const char *argv[])
{
Test t;
t.set(10, 9);
t.print();
return 0;
}