创建一个vector容器,并向该容器中放入10个数,通过使用迭代器的方式输出10个元素。
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> v;
int a;
for (vector<int>::size_type i = 0; i < 10; i++)
{
cin >> a;
v.push_back(a);
}
vector<int>::iterator iter = v.begin();
while (iter != v.end()) {
cout << *iter << ' ';
iter++;
}
cout << endl;
return 0;
}
设计一个类模板Sample,用于对一个有序数组采用二分法查找元素下标
#include <iostream>
using namespace std;
template <class Sample>
class Array
{
private:
Sample* x;
int n;
public:
Array(Sample* a, int b)
{
x = a;
n = b;
}
void Search(Sample key)
{
bool isFind = false;
int low = 0, mid, high = n - 1;
for (; high >= low;)
{
mid = (low + high) / 2;
if (key == x[mid])
{
cout << "该数是数组中第" << 1 + mid << "个元素的值" << endl;
isFind = true;
break;
}
else if (key < x[mid])
high = mid - 1;
else
low = mid + 1;
}
if (!isFind)
cout << "无此数" << endl;
}
};
int main()
{
int a[] = { 1,3,5,7,9,11 };
Array<int>t1(a, 6);
t1.Search(9);
return 0;
}
有两组数据分别是{10,15,2,8,22,11}、{1,5,10,22,4,8,33,6},将这两组数据分别存储在list容器中,并将两个链表合并,合并之后删除重复元素,对链表中的值从小到大排序。
#include<iostream>
#include<list>
using namespace std;
int main() {
int a[6] = { 10,15,2,8,22,11 };
int b[8] = { 1,5,10,22,4,8,33,6 };
list<int> a_list(a, a + 6);
list<int> b_list(b, b + 8);
list<int>::iterator iter;
cout << "第一个链表为:";
for (iter = a_list.begin(); iter != a_list.end(); iter++)
cout << *iter << " ";
cout << endl << "第二个链表为:";
for (iter = b_list.begin(); iter != b_list.end(); iter++)
cout << *iter << " ";
cout << endl << "合并之后的链表为:";
a_list.splice(a_list.end(), b_list);
for (iter = a_list.begin(); iter != a_list.end(); iter++)
cout << *iter << " ";
cout << endl << "删除重复元素后小到大排序的链表为:";
a_list.sort();
a_list.unique();
for (iter = a_list.begin(); iter != a_list.end(); iter++)
cout << *iter << " ";
cout << endl;
return 0;
}
编写冒泡排序的函数模板
#include <iostream>
using namespace std;
int n;
template <typename T>
void get_num(T* a)
{
cout << "请输入需要排序的数的个数:" << endl;
cin >> n;
cout << "请输入需要排序的数:" << endl;
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
}
template <typename T>
void sort(T* a)
{
T tmp;
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - 1 - i; j++)
{
if (a[j] > a[j + 1])
{
tmp = a[j];
a[j] = a[j + 1];
a[j + 1] = tmp;
}
}
}
}
template <typename T>
void print(T* a)
{
cout << "按从小到大排序后为:" << endl;
for (int i = 0; i < n; i++)
{
cout << a[i] << " ";
}
cout << endl;
}
int main()
{
int a[100] = { 0 };
get_num<int>(a);
sort<int>(a);
print<int>(a);
return 0;
}
将Person自定义数据类型进行排序,Person中属性有姓名、年龄、身高
排序规则:按照年龄进行升序,如果年龄相同按照身高进行降序
#include<iostream>
#include<list>
#include<string>
using namespace std;
class Person
{
public:
string Name;
int Age;
int Height;
Person(string name, int age, int height)
{
this->Age = age;
this->Height = height;
this->Name = name;
}
};
bool comparePerson(Person& p1, Person& p2)
{
if (p1.Age == p2.Age)
return p1.Height > p2.Height;
else
return p1.Age < p2.Age;
}
void test01()
{
list<Person>List;
Person p1("张一", 18, 187);
Person p2("张二", 20, 176);
Person p3("张三", 20, 180);
Person p4("张四", 34, 177);
Person p5("张五", 27, 183);
List.push_back(p1);
List.push_back(p2);
List.push_back(p3);
List.push_back(p4);
List.push_back(p5);
cout << "排序前为:" << endl;
for (list<Person>::const_iterator it = List.begin(); it != List.end(); it++)
{
cout << "姓名:" << (*it).Name << " 年龄:" << (*it).Age << " 身高:" << (*it).Height << endl;
}
cout << endl << "排序后为: " << endl;
List.sort(comparePerson);
for (list<Person>::const_iterator it = List.begin(); it != List.end(); it++)
{
cout << "姓名:" << (*it).Name << " 年龄:" << (*it).Age << " 身高:" << (*it).Height << endl;
}
}
int main()
{
test01();
return 0;
}
编写一个程序将data.dat文件中的内容在屏幕上显示出来并复制到data1.dat文件中。
(此题需要在文件夹里先建对应的文件data.dat才能够执行)
#include <iostream>
#include<fstream>
#include<cstdlib>
using namespace std;
int main()
{
FILE* fp1, * fp2;
char ch;
if ((fp1 = fopen("data.dat", "r")) == NULL)
{
perror("Open file");
return -1;
}
if ((fp2 = fopen("data1.dat", "w")) == NULL)
{
perror("Open file");
return -1;
}
while (!feof(fp1))
{
ch = fgetc(fp1);
putchar(ch);
fputc(ch, fp2);
}
cout << endl;
fclose(fp1);
fclose(fp2);
return 0;
}
编写一个程序统计文件abc.txt的字符个数。
(此题需要在文件夹里先建对应的文件abc.txt并且在里面输入随机字符才能够执行)
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
fstream f("abc.txt", ios::in);
char c;
int n = 0;
while (f.get(c))n++;
cout << n << endl;
f.close();
return 0;
}
编写一个程序将abc.txt文件的所有行加上行号后写到abc1.txt文件中。
(此题需要在文件夹里先建对应的文件abc.txt以及abc1.txt才能够执行)
#include<iostream>
#include<cstdlib>
#include<fstream>
using namespace std;
int main()
{
fstream outfile, infile;
infile.open("abc.txt", ios::in);
if (!infile)
{
cout << "abc.txt不能被打开" << endl;
abort();
}
outfile.open("abc1.txt", ios::out);
if (!outfile)
{
cout << "abc.txt不能被打开" << endl;
abort();
}
char buf[80];
int i = 1;
while (!infile.eof())
{
infile.getline(buf, 80);
outfile << i++ << ":" << buf << endl;
}
outfile.close();
infile.close();
return 0;
}