istream_iterator:输入迭代器。
vector<int> V;
copy (istream_iterator<int>(cin), istream_iterator<int>(), back_inserter(V));
ostream_iterator:输出迭代器。
vector<int> V;
//…
copy (V.begin(), V.end(), ostream_iterator<int>(cout, "\n"));
//输出迭代器
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
void main()
{
vector<int> walf;
int element;
while (cin >> element)
walf.push_back (element);
copy(walf.begin(), walf.end(), ostream_iterator<int>(cout, "\n"));
}
/*vector基本操作及演示*/
//创建空向量
#include <vector>
#include <cassert>
#include <iostream>
using namespace std;
int main()
{
cout << "演示如何创建向量" << endl;
vector<char> myVector;
assert (myVector.size() == 0);
//检测该向量是否为空
cout << "结果正确" << endl;
system ("pause");
return 0;
}
//插入操作
#include <vector>
#include <string>
#include <cassert>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
char a[] = "I love football";
char b[] = "llabtoof evol I";
//数组b中元素为a中元素的倒序
vector<char> myVector1 (a, a + strlen(a)), myVector2;
vector<char> myVector3 (b, b + strlen(b)), myVector4;
vector<char> ::iterator iter;
cout << "向量成员函数push_back()示例:" << endl;
for(iter = myVector1.begin(); iter != myVector1.end(); ++iter)
myVector2.push_back(*iter);
//在myVector4起点处依次插入myVector1的元素
assert (myVector1 == myVector2);
cout << "在向量起点插入元素示例:" << endl;
for(iter = myVector1.begin(); iter != myVector1.end(); ++iter)
myVector4.insert(myVector4.begin(), *iter);
//在myVector起点处依次插入myVector1的元素
assert(myVector4 == myVector3);
//通过泛型算法 reverse()翻转myVector1
//来验证myVector4是myVector1的翻转
reverse (myVector1.begin(),myVector1.end());
assert(myVector1 == myVector4);
cout << "结果正确" << endl;
system ("pause");
return 0;
}
//删除操作
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
vector<int> vector1;
vector<int>::iterator iter;
//将从0到9的元素依次放入vector1中
for(int i = 0; i < 10; ++i)
vector1.push_back(i);
cout << "当前向量中的元素:";
for(iter = vector1.begin(); iter != vector1.end(); ++iter)
cout << *iter;
cout << endl;
//删除最后一个元素 9
vector1.pop_back();
cout << "删除9后的向量长度:" << vector1.size() << endl;
//删除第一个元素 0
vector1.erase(vector1.begin());
cout << "再删除0后的向量元素:" << vector1.size() << endl;
//删除数值为5的元素
remove (vector1.begin(), vector1.end(),5);
cout << "再删除5后,当前向量中的元素:";
for(iter = vector1.begin(); iter != vector1.end(); ++iter)
cout << *iter;
cout << endl;
//删除所有剩下的元素
vector1.erase(vector1.begin(), vector1.end());
cout << "删除所有剩下元素长度:" << vector1.size() << endl;
system ("pause");
return 0;
}
//查找
#include <vector>
#include <cassert>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
char str[] = "I am a good girl";
//用已有字符数组初始化向量
vector<char> myVector(str, str + strlen(str));
//查找第一次出现的元素g
vector<char>::iterator \
where = find (myVector.begin(), myVector.end(), 'g');
assert (*where == 'g' && *(where + 1) == 'o');
cout << "查找结果正确!" << endl;
system("pause");
return 0;
}
#include <vector>
#include <cassert>
#include <iostream>
#include <algorithm>
using namespace std;
//定义一个函数对象类
class GreatorThan30
{
public:
bool operator() (int x) const { return x > 30; }
};
int main()
{
vector<int> myVector;
vector<int>:: iterator iter;
//向量元素值为0至9的平方
for(int i = 0; i < 10; ++i)
myVector.push_back(i * i);
cout << "向量中所包含元素:";
for(iter = myVector.begin(); iter !=myVector.end(); ++iter)
cout << *iter << " ";
cout << endl;
//查找第一个大于30的元素
vector<int> :: iterator where;
where = find_if (myVector.begin(), myVector.end(), GreatorThan30());
//结果输出
if(where == myVector.end())
cout << "没有找到大于30的数!" << endl;
else
cout << "第一个大于30的数为:" << *where << endl;
system ("pause");
return 0;
}
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
int main(void)
{
vector<char> targetChar;
vector<char> vectorOfChar;
//生成需要查找的字符串,单词blue
targetChar.push_back('b');
targetChar.push_back('l');
targetChar.push_back('u');
targetChar.push_back('e');
//生成被查找向量
vectorOfChar.push_back('r');
vectorOfChar.push_back('e');
vectorOfChar.push_back('d');
vectorOfChar.push_back('b');
vectorOfChar.push_back('l');
vectorOfChar.push_back('u');
vectorOfChar.push_back('e');
//在redblue中查找blue
vector<char>::iterator where =
search(vectorOfChar.begin(), vectorOfChar.end(),
targetChar.begin(), targetChar.end());
if(where != vectorOfChar.end())
cout << "找到了单词blue!" << endl;
else
cout << "没有找到单词blue" << endl;
system("pause");
return 0;
}
//遍历输出
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
//定义一个(狭义的)函数对象
void PrintF(string& StringPrint)
{
cout << StringPrint << endl;
}
int main(void)
{
//构造一个向量,其元素为一周的天数
vector<string> week;
week.push_back (" Monday ");
week.push_back(" Tuesday ");
week.push_back(" Wednesday ");
week.push_back(" Thursday ");
week.push_back(" Friday ");
week.push_back(" Saturday ");
week.insert(week.begin()," Sunday ");
//遍历输出
for_each(week.begin(), week.end(), PrintF); //PrintF实际是函数指针
system ("pause");
return 0;
}
//判断空向量
#include <vector>
#include <iostream>
using namespace std;
int main()
{
vector<char> myVector;
cout << "演示如何创建向量" << endl;
//检验该向量是否为空
if(myVector.empty() == 1)
cout << "向量为空!" << endl;
else
cout << "向量不为空" << endl;
system ("pause");
return 0;
}
//向量排序
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
void PrintF (string& StringPrint)
{
cout << StringPrint << endl;
}
int main(void)
{
vector<string> studentName;
vector<string>:: iterator studentIterator;
studentName.push_back("王二");
studentName.push_back("欣欣");
studentName.push_back("外星人");
studentName.push_back("花");
studentName.push_back("乐乐");
studentName.push_back("日向雏田");
//输出未排序的学生名单
cout << "排序前的学生名单" << endl;
for_each(studentName.begin(), studentName.end(), PrintF);
//排序并输出排序后的学生名单
sort (studentName.begin(), studentName.end());
cout << "排序后的学生名单" << endl;
for_each(studentName.begin(), studentName.end(), PrintF);
system ("pause");
return 0;
}
//元素计数
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
class IsFull
{
public:
bool operator() (int & score)
{
return score == 100;
}
};
int main()
{
vector<int> Score;
//构造成绩单
Score.push_back(67); Score.push_back(89);
Score.push_back(100); Score.push_back(86);
Score.push_back(56); Score.push_back(100);
Score.push_back(99); Score.push_back(74);
int NFullScore;
NFullScore = count(Score.begin(), Score.end(), 100);
cout << "用count‘()计数:" << endl;
cout << "There were" << NFullScore << "fullscores!" << endl;
int NFullScoreF;
NFullScoreF = count_if(Score.begin(), Score.end(), IsFull());
cout << "用count_if计数" << endl;
cout << "There were" << NFullScoreF << "fullscores!" << endl;
system ("pause");
return 0;
}
/*list基本操作及演示*/
//插入和删除
#include <list>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int m = 'a'; //隐式类型转换
list<char> myList;
list<char>::iterator link_iter;
vector<char> myVec;
vector<char>:: iterator v_iter;
//将字符abcdef放入表中
for(int i = 0; i < 6; i++)
{
//隐式类型转换,整数转换为ASCII码
myList.push_back(m + i);
myVec.push_back(m + i);
}
//判断删除字符后的迭代器是否有效
link_iter = find(myList.begin(), myList.end(), 'c');
cout << "删除link中的一个节点元素" << endl;
myList.erase(link_iter++);
if(*link_iter == 'd')
cout << "此时迭代器所指示元素为:"
<< *link_iter
<< endl
<< "删除元素之后,迭代器保持有效"
<< endl
<<endl;
v_iter = find(myVec.begin(), myVec.end(), 'c');
cout << "删除vctor中一个节点元素" << endl;
myVec.erase (v_iter++);
if(*v_iter == 'd')
cout << *v_iter
<< endl
<< "迭代器保持有效" << endl;
else
cout << "被删除元素之后的迭代器无效" << endl;
system ("pause");
return 0;
}
//删除相邻且满足一定条件的元素
#include <List>
#include <iostream>
#include <algorithm>
using namespace std;
void PrintF(int& PrintInt)
{
cout << PrintInt << endl;
};
//定义一个作为unique()比较操作符的类
class EvenPair{
public:
bool operator() (int div1, int div2)
{
return ! (div2 % div1);
}
};
int main()
{
int myArray[] = {5, 2, 9, 4, 8, 2, 3, 4, 9};
list<int> myList1(myArray, myArray + 9);
list<int> myList2(myList1);
//删除相同的相邻元素
myList1.unique();
cout << "删除后的结果为:" << endl;
for_each(myList1.begin(), myList1.end(), PrintF);
//排序后删除相同的相邻元素
myList1.sort();
myList1.unique(); //删除2,4,9
cout << "排序删除后的结果为: " << endl;
for_each(myList1.begin(), myList1.end(), PrintF);
myList2.unique(EvenPair()); //删除8
cout << "删除第二个元素能被第一个整除的相邻元素后的结果为:" << endl;
for_each(myList2.begin(), myList2.end(), PrintF);
system("pause");
return 0;
}
//排序
#include <list>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
void PrintF (string& StringPrint)
{
cout << StringPrint << endl;
}
int main(void)
{
list<string> studentName;
list<string>::iterator studentIterator;
studentName.push_back("日向雏田");
studentName.push_back("王二");
studentName.push_back("焦嘉欣");
studentName.push_back("曾婷");
studentName.push_back("小林光一");
studentName.push_back("马拉多纳");
//输出未排序的学生名单
cout << "排序前的学生名单:" << endl;
for_each(studentName.begin(), studentName.end(), PrintF);
//排序并输出排序后的学生名单
studentName.sort();
cout << "排序后的学生名单" << endl;
for_each(studentName.begin(), studentName.end(),PrintF);
system("pause");
return 0;
}
#if 0
拼接和合并
#endif
#include <list>
#include <iostream>
#include <algorithm>
using namespace std;
void PrintF(int& PrintInt)
{
cout << PrintInt << endl;
}
int main()
{
#if 1
cout << "我是王二" << endl;
#endif
int myArray[] = {23, 45, 89, 43, 56, 27, 81, 10, 51, 32, 78, 74};
list<int> myList1(myArray, myArray + 12);
list<int> myList2;
list<int>:: iterator\
iter1 = find(myList1.begin(), myList1.end(), 45);
//使迭代器指向myList1的第2个元素
list<int>::iterator\
iter2 = find(myList1.begin(), myList1.end(), 81);
//使迭代器指向myList1的第7个元素
list<int> myList3(iter2, myList1.end());
//用myList1第7个元素之后的区间构造一个新表
//结合一个元素
myList2.splice(myList2.end(),myList1,myList1.begin());
cout << "将myList的第一个元素移到myList2的结果:" << endl;
for_each(myList2.begin(), myList2.end(), PrintF);
//结合一个区间
//必须注意这里的iter2所指向的元素并不参加操作
myList2.splice(myList2.end(), myList1, iter1, iter2);
cout << "继续将mylist1的第2个至第6个元素移到myList2的结果" << endl;
for_each(myList2.begin(), myList2.end(), PrintF);
//结合myList3的所有元素
myList2.splice(myList2.end(), myList3);
cout << "继续将myList3的全部元素移至myList2中去的结果:" << endl;
for_each(myList2.begin(), myList2.end(), PrintF);
system("pause");
return 0;
}
C++ STL
最新推荐文章于 2014-01-05 18:36:11 发布