//NString.h
#ifndef NSTRING_H
#define NSTRING_H
#include <algorithm>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
typedef std::pair<std::string, int> psi;
bool operator==(const psi& l, const psi& r)
{
return l.first == r.first;
}
class NString
{
std::string s;
int thisOccurrence;
typedef std::vector<psi> vp;
typedef vp::iterator vpit;
static vp words;
void addString(const std::string &x)
{
psi p(x, 0);
vpit it = std::find(words.begin(), words.end(), p);
if(it != words.end())
thisOccurrence = ++it->second;
else
{
thisOccurrence = 0;
words.push_back(p);
}
}
public:
NString() : thisOccurrence(0) {}
NString(const std::string &x) : s(x)
{
addString(x);
}
NString(const char* x) : s(x)
{
addString(x);
}
friend std::ostream&
operator<<(std::ostream &os, const NString &ns)
{
return os << ns.s << " [" << ns.thisOccurrence << "]";
}
friend bool
operator<(const NString &l, const NString &r)
{
return l.s < r.s;
}
friend
bool operator==(const NString &l, const NString &r)
{
return l.s == r.s;
}
friend bool
operator>(const NString &l, const NString &r)
{
return l.s > r.s;
}
operator const std::string&()const
{
return s;
}
};
NString::vp NString::words;
#endif
//Manipulations.cpp
#include <vector>
#include <string>
#include <algorithm>
#include "PrintSequence.h"
#include "NString.h"
#include "Generators.h"
using namespace std;
int main()
{
vector<int> v1(10);
generate(v1.begin(), v1.end(),SkipGen());
print(v1.begin(), v1.end(), "v1", " ");
vector<int> v2(v1.size());
copy_backward(v1.begin(), v1.end(), v2.end());//目的序列范围的空间必须已经存在,第一个目的元素是v2.end() - 1
print(v2.begin(), v2.end(), "copy_backward", " ");
vector<int> v3(v1.size());
copy(v1.begin(), v1.end(), v3.begin());//
print(v3.begin(), v3.end(), "copy", " ");
reverse_copy(v1.begin(), v1.end(), v2.begin());
print(v2.begin(), v2.end(), "reverse_copy", " ");
reverse(v1.begin(), v1.end());
print(v1.begin(), v1.end(), "reverse", " ");
int half = v1.size() / 2;
swap_ranges(v1.begin(), v1.begin() + half, v1.begin() + half);
print(v1.begin(), v1.end(), "swap_ranges", " ");
generate(v1.begin(), v1.end(), SkipGen());
print(v1.begin(), v1.end(), "v1", " ");
int third = v1.size() / 3;
for(int i = 0; i < 10; i++)
{
rotate(v1.begin(), v1.begin() + third, v1.end());
print(v1.begin(), v1.end(), "rotate", " ");
}
cout << "Second rotate example:" << endl;
char c[] = "aabbccddeeffgghhiijj";
const char CSZ = strlen(c);
for(int i = 0; i < 10; i++)
{
rotate(c, c + 2, c + CSZ);
print(c, c + CSZ, "", "");
}
cout << "All n! permutations of abcd:" << endl;
int nf = 4 * 3 * 2 * 1;
char p[] = "abcd";
for(int i = 0; i < nf; i++)
{
next_permutation(p, p + 4);
print(p, p + 4, "", "");
}
cout << "Using prev_permutation:" << endl;
for(int i = 0; i < nf; i++)
{
prev_permutation(p, p + 4);
print(p, p + 4, "", "");
}
cout << "randoms_shuffling a word:" << endl;
string s("hello");
for(int i = 0; i < 5; i++)
{
random_shuffle(s.begin(), s.end());
cout << s << endl;
}
NString sa[] =
{
"a", "b", "c", "d", "a",
"b", "c", "d", "a", "b",
"c", "d", "a", "b", "c"
};
const int SASZ = sizeof sa / sizeof *sa;
vector<NString> ns(sa, sa + SASZ);
print(ns.begin(), ns.end(), "ns", " ");
vector<NString>::iterator it =
partition(ns.begin(), ns.end(),
bind2nd(greater<NString>(), "b"));
cout << "Partition point: " << *it << endl;
print(ns.begin(), ns.end(), "", " ");
copy(sa, sa + SASZ, ns.begin());
it = stable_partition(ns.begin(), ns.end(),
bind2nd(greater<NString>(), "b"));
cout << "Stable partition" << endl;
cout << "Partition point: " << *it << endl;
print(ns.begin(), ns.end(), "", " ");
system("pause");
return 0;
}