1。有一个vector<int> 其中有值 0,1,0,2,2,3,1请删除重复的元素并从小到大排序(建议使用泛型算法等)。(泛型算法)。
2.编写一段程序,从标准输入中读取若干string对象并查找连续重复出现的单词,记录出现最大连续出现次数和对应的对象。如果没有连续重复出现,输出说明信息。
3.编写程序,将一个list中的char* 指针(指向C风格字符串)元素赋值给一个vector中的string
4.将来自一个文件(a.txt)的行保存在一个vector<string>中,然后使用一个istringstream从vector读取数据元素,每次读取一个单词,并且打印出来。(C++ primer8.10)(IO文件操作)
代码
#include <iostream>
using namespace std;
//拷贝赋值运算符
/*
默认的拷贝赋值运算符:将右侧运算对象的每个非static成员赋予左侧运算对象的对应成员,这一工作是通过成员类型的拷贝赋值运算符来完成。对于数组类型成员,逐个赋值数组元素。合成拷贝赋值运算符返回一个指向其左侧运算对象的引用。
*/
//只要定义构造函数,默认构造函数就被delete
class MyClass1
{
public:
//拷贝赋值运算符
MyClass1& operator=(const MyClass1 &my)
{
this->x = my.x;
cout << "MyClass1& operator=(const MyClass1 &my)" << endl;
return *this;
}
MyClass1(const MyClass1 &my)
{
cout << " MyClass1(const MyClass1 &my)" << endl;
}
MyClass1() = default;
int x;
};
#include <vector>
#include <algorithm>
void test1()
{
vector<int> myv{ 0, 1, 0, 2, 2, 3, 1 };
auto it=unique(myv.begin(), myv.end());
cout << *it << endl;
sort(myv.begin(), myv.end());
for (auto ib = unique(myv.begin(), myv.end()); ib != myv.end(); ib++)
{
cout << *ib;
}
//unique去掉重复的,然后元素前移,返回值为未元素所在的位置(尾元素已经前移了),此时返回值指向了无效数据,需要移除。
myv.erase(unique(myv.begin(), myv.end()), myv.end());
//去掉重复的元素:先排序sotr(begin,end);unique去重;利用去重的返回值从集合中移除myv.erase();
}
void test2()
{
cout << "请输入两个整数" << endl;
int min=0, max=0;
cin >> min >> max;
if (min>max)
{
int i = min;
min = max;
max = i;
}
for (int i = min+1; i < max; i++)
{
cout << i << " ";
}
}
#include <string>
void test3()
{
int num = 1;
vector<string> vec;
vector<string> strName;
vector<int> strCnt;
string str;
int max = 0, flag = 0;
cout << "输入end结束输入" << endl;
while (cin>>str && str!="end")
{
vec.push_back(str);
str.clear();
}
string temp = *vec.begin();
for (auto it = vec.begin()+1; it != vec.end(); it++)
{
if (temp == *it)
num++;
else
{
strName.push_back(temp);
strCnt.push_back(num);
temp = *it;
num = 1;
}
if (it+1==vec.end())
{
strName.push_back(temp);
strCnt.push_back(num);
}
}
max = *strCnt.begin();
for (int i = 0; i < strCnt.size(); i++)
{
if (max < strCnt[i])
{
max = strCnt[i];
flag = i;
}
}
for (int i = 0; i < strName.size(); i++)
{
cout << "连续出现的字符串的名称是:" << strName[i] << " " << "出现次数是:" << strCnt[i] << endl;
}
cout << "连续出现的最大字符串的名称是:" << strName[flag] << " " << "出现次数是:" << strCnt[flag] << endl;
}
#include <list>
void test4()
{
list<char*> cli;
vector<string> svec;
char s1[] = "first";
char s2[] = "second";
char s3[] = "third";
char s4[] = "fourth";
cli.push_back(s1);
cli.push_back(s2);
cli.push_back(s3);
cli.push_back(s4);
copy(cli.begin(), cli.end(), back_inserter(svec));
//copy(svec.begin(), svec.end(), ostream_iterator<string>(cout, "\t"));
}
#include <fstream>
#include<sstream>
void test5(string filename="eng.txt")
{
string line, word;
ifstream in(filename);
vector<string> line_vec, word_vec;
while (!in.eof()) //从文件里读取内容,第一层
{
getline(in, line);
line_vec.push_back(line); //存放一行一行的string
}
for (auto i = 0; i != line_vec.size(); ++i)
{
istringstream sin(line_vec[i]);
//第二层,读取一行一行的string
while (sin >> word)
//一个单词一个单词读取
{
word_vec.push_back(word);
//存放一个一个的单词
}
}
for (auto i = 0; i != word_vec.size(); ++i) //打印单词
cout << word_vec[i] << endl;
}
void main()
{
test1();
cin.get();
}
void main1()
{
MyClass1 my1;
MyClass1 my2 = my1;//调用拷贝构造函数。
my2 = my1;//赋值运算符
cin.get();
}