コードの量については事前にお詫びしますが、ビルド エラーの原因がわかりません。この例は VC++ 6.0 で正しくビルドされましたが、g++ や VC++ 2010 を含む最近のコンパイラでは失敗しました。 class 'std::basic_ios<_Elem,_Traits>' 助けてください。
#pragma warning( disable : 4786 )
#pragma warning( disable : 4996 )
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <list>
#include <numeric>
#include <iterator>
template<class T>
class Criteria {
public:
Criteria(T match) : m_match(match) {
}
bool operator()(T match) {
return (m_match == match);
}
private:
T m_match;
};
template<class T>
class BinaryCriteria {
public:
bool operator()(T item1, T item2) {
return (item1*item1 > item2+item2);
}
};
std::ostream& operator<<(std::ostream& o, std::string rhs) {
return (o << rhs.c_str());
}
void print( int x ) {
std::cout << x << " ";
}
template<class T>
class Print {
public:
Print(const char* sep) : m_sep(sep) {
}
void operator()(T item) {
std::cout << item << m_sep.c_str();
}
private:
std::string m_sep;
};
template<class T>
class Equal {
public:
bool operator()(T item1, T item2) {
return (item1 * 2 == item2);
}
};
template<class T>
class File {
public:
File(const char* file) {
m_in.open(file);
}
~File() {
if (m_in)
m_in.close();
}
T operator()() {
m_in.getline(m_buf, sizeof(m_buf));
T line = m_buf;
return line;
}
private:
std::ifstream m_in;
char m_buf[80];
};
template<class T>
class Odd {
public:
bool operator()(T item) {
if (item % 2 != 0)
return false;
else
return true;
}
};
template<class T>
class Upper {
public:
T operator()(T item) {
std::transform(item.begin(),item.end(),item.begin(),::toupper);
return item;
}
};
template<class T>
class Concat {
public:
Concat(const char* sep) : m_sep(sep) {
}
T operator()(T item1, T item2) {
T out;
out = item1;
out += m_sep;
out += item2;
return out;
}
private:
std::string m_sep;
};
template<class T>
class TheSame {
public:
bool operator()(T item1, T item2) {
if (item1 * 10 == item2)
return true;
else
return false;
}
};
template<class T>
class Rand {
public:
T operator()() {
return static_cast<T>(::rand());
}
};
struct Employee {
Employee(std::string name, std::string dept, std::string id) :
m_name(name), m_dept(dept), m_id(id) {
}
std::string m_name;
std::string m_dept;
std::string m_id;
};
class SortByName {
public:
bool operator()(const Employee* lhs, const Employee* rhs) {
int res = ::strcmp(lhs->m_name.c_str(),rhs->m_name.c_str());
if (res == -1)
return true;
else
return false;
}
};
class SortByDept {
public:
bool operator()(const Employee* lhs, const Employee* rhs) {
int res = ::strcmp(lhs->m_dept.c_str(),rhs->m_dept.c_str());
if (res == -1)
return true;
else
return false;
}
};
class SortById {
public:
bool operator()(const Employee* lhs, const Employee* rhs) {
int res = ::strcmp(lhs->m_id.c_str(),rhs->m_id.c_str());
if (res == -1)
return true;
else
return false;
}
};
std::ostream& operator<<(std::ostream& o, const Employee* rhs) {
o << rhs->m_name << "," << rhs->m_dept << "," << rhs->m_id;
return o;
}
int main(int argc, char* argv[]) {
if (false) {
std::vector<int> v;
for (int i=0; i<10; i++)
v.push_back(i);
std::vector<int>::iterator ifind = std::find(v.begin(), v.end(), -4);
if (ifind != v.end())
std::cout << "Item found " << *ifind << std::endl;
else
std::cout << "Item was not found" << std::endl;
ifind = std::find_if(v.begin(), v.end(), Criteria<int>(4));
if (ifind != v.end())
std::cout << "Item found " << *ifind << std::endl;
else
std::cout << "Item was not found" << std::endl;
}
if (false) {
std::vector<int> v;
for (int i=0; i<10; i++)
v.push_back(i);
v[4] = 5; // create an adjacent pair
std::vector<int>::iterator ifind = std::adjacent_find(v.begin(), v.end());
if (ifind != v.end())
std::cout << "Items found " << *ifind << " " << *(ifind+1) << std::endl;
else
std::cout << "Items were not found" << std::endl;
v[4] = 4; // create an adjacent pair
ifind = std::adjacent_find(v.begin(), v.end(), BinaryCriteria<int>());
if (ifind != v.end())
std::cout << "Items found " << *ifind << " " << *(ifind+1) << std::endl;
else
std::cout << "Items were not found" << std::endl;
}
if (false) {
std::vector<int> v;
for (int i=0; i<10; i++)
v.push_back(i);
v[3] = 3;
v[7] = 3; // some duplicates
std::cout << "The item " << 2 << " appears "
<< std::count( v.begin(), v.end(), 2)
<< " times" << std::endl;
std::cout << "The item " << 3 << " appears "
<< std::count_if( v.begin(), v.end(), Criteria<int>(3))
<< " times" << std::endl;
}
if (false) {
std::vector<int> v;
for (int i=0; i<10; i++)
v.push_back(i);
std::for_each(v.begin(), v.end(), Print<int>(" "));
std::cout << std::endl;
std::for_each(v.begin(), v.end(), print);
std::cout << std::endl;
}
if (false) {
std::vector<int> v;
for (int i=0; i<10; i++)
v.push_back(i);
int tab[3] = { 0, 1, 2 };
bool res = std::equal(tab, tab+3, v.begin());
if (res)
std::cout << "Ranges are equal" << std::endl;
else
std::cout << "Ranges are not equal" << std::endl;
v[0] = 2 * tab[0];
v[1] = 2 * tab[1];
v[2] = 2 * tab[2];
res = std::equal(tab, tab+3, v.begin(), Equal<int>());
if (res)
std::cout << "Ranges are equal" << std::endl;
else
std::cout << "Ranges are not equal" << std::endl;
res = std::equal(tab, tab+3, v.begin());
if (res)
std::cout << "Ranges are equal" << std::endl;
else
std::cout << "Ranges are not equal" << std::endl;
res = std::equal(tab, tab+3, &v[5], Equal<int>());
if (res)
std::cout << "Ranges are equal" << std::endl;
else
std::cout << "Ranges are not equal" << std::endl;
}
if (false) {
std::vector<int> v;
for (int i=0; i<10; i++)
v.push_back(i);
int tab[6] = { 0, 1, 2, 4, 5, 6 };
std::pair<int*, std::vector<int>::iterator> res =
std::mismatch(tab, tab+6, v.begin() );
if (res.first != (tab+6)) {
std::cout << "mismatch located "
<< "item in first range " << *res.first <<
" item in second range " << *res.second << std::endl;
} else
std::cout << "No mismatch found within the range" << std::endl;
v[0] = 2 * tab[0];
v[1] = 2 * tab[1];
v[2] = 2 * tab[2];
tab[3] = -99;
res = std::mismatch(tab, tab+6, v.begin(), Equal<int>() );
if (res.first != (tab+6)) {
std::cout << "mismatch located "
<< "item in first range " << *res.first <<
" item in second range " << *res.second << std::endl;
} else
std::cout << "No mismatch found within the range" << std::endl;
}
if (false) {
std::vector<int> v;
for (int i=0; i<10; i++)
v.push_back(i);
int tab[3] = { 4, 5, 6 };
std::vector<int>::iterator res = std::search(v.begin(), v.end(), tab, tab+3);
if (res != v.end())
std::cout << "Subsequence located - first item = " << *res << std::endl;
else
std::cout << "Subsequence not located" << std::endl;
tab[0] = 2 * v[5];
tab[1] = 2 * v[6];
tab[2] = 2 * v[7];
// predicate function version
res = std::search(v.begin(), v.end(), tab, tab+3, Equal<int>());
if (res != v.end())
std::cout << "Subsequence located - first item = " << *res << std::endl;
else
std::cout << "Subsequence not located" << std::endl;
}
if (false) {
std::vector<int> v(10);
int tab[10] = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
std::copy(tab,tab+10,v.begin());
std::for_each(v.begin(),v.end(),Print<int>(" "));
std::cout << std::endl;
std::copy_backward(tab,tab+10,v.end());
std::for_each(v.begin(),v.end(),Print<int>(" "));
std::cout << std::endl;
std::vector<char> v2;
const char* str = "ABCDEFGHIJ";
std::copy(str,str+10,std::back_inserter(v2));
std::cout << "Before shift left" << std::endl;
std::for_each(v2.begin(),v2.end(),Print<char>(""));
std::cout << std::endl;
std::copy(v2.begin()+1, v2.end(), v2.begin());
std::cout << "After shift left" << std::endl;
std::for_each(v2.begin(),v2.end(),Print<char>(""));
std::cout << std::endl;
std::copy(str,str+10,v2.begin());
std::cout << "Before shift right" << std::endl;
std::for_each(v2.begin(),v2.end(),Print<char>(""));
std::cout << std::endl;
std::copy_backward(v2.begin(), v2.end()-1, v2.end());
std::cout << "After shift right" << std::endl;
std::for_each(v2.begin(),v2.end(),Print<char>(""));
std::cout << std::endl;
}
if (false) {
std::vector<int> v(10);
std::fill(v.begin(),v.end(),-5);
std::for_each(v.begin(),v.end(),Print<int>(" "));
std::cout << std::endl;
std::fill_n(v.begin(),10,-5);
std::for_each(v.begin(),v.end(),Print<int>(" "));
std::cout << std::endl;
}
if (false) {
std::vector<std::string> v(10);
std::generate(v.begin(),v.end(),File<std::string>("c:/temp/items.txt"));
std::for_each(v.begin(),v.end(),Print<std::string>("\n"));
std::cout << std::endl;
}
if (false) {
std::vector<int> v;
for(int i=0; i<10; i++)
v.push_back(i);
std::vector<int>::iterator res =
std::partition(v.begin(),v.end(),Odd<int>());
// display each range
std::cout << "first range ";
std::for_each(v.begin(),res,Print<int>(" "));
std::cout << std::endl;
std::cout << "second range ";
std::for_each(res,v.end(),Print<int>(" "));
std::cout << std::endl;
// repeat for stable_partition
for(int i=0; i<10; i++)
v[i]=i;
res = std::stable_partition(v.begin(),v.end(),Odd<int>());
// display each range
std::cout << "first range ";
std::for_each(v.begin(),res,Print<int>(" "));
std::cout << std::endl;
std::cout << "second range ";
std::for_each(res,v.end(),Print<int>(" "));
std::cout << std::endl;
// repeat using STL components
for(int i=0; i<10; i++)
v[i]=i;
res = std::partition(v.begin(),v.end(),
std::bind2nd( std::greater<int>(), 2));
// display each range
std::cout << "first range ";
std::for_each(v.begin(),res,Print<int>(" "));
std::cout << std::endl;
std::cout << "second range ";
std::for_each(res,v.end(),Print<int>(" "));
std::cout << std::endl;
// repeat using STL components
for(int i=0; i<10; i++)
v[i]=i;
res = std::partition(v.begin(),v.end(),
std::bind1st( std::greater<int>(), 2));
// display each range
std::cout << "first range ";
std::for_each(v.begin(),res,Print<int>(" "));
std::cout << std::endl;
std::cout << "second range ";
std::for_each(res,v.end(),Print<int>(" "));
std::cout << std::endl;
}
if (false) {
// random_shuffle randomly rearranges the items in a range.
std::vector<int> v;
for(int i=0; i<100; i++)
v.push_back(i);
std::random_shuffle(v.begin(),v.end());
std::copy(v.begin(),v.end(),
std::ostream_iterator<int>(std::cout," "));
std::cout << std::endl;
}
if (false) {
int buf[10] = { 2, 5, 3, 5, 4, 5, 6, 6, 5, 5 };
int* res = std::remove(buf, buf+10, 5);
std::cout << "items to keep ";
std::copy(buf, res, std::ostream_iterator<int>(std::cout," "));
std::cout << std::endl;
std::cout << "items to remove ";
std::copy(res, buf+10, std::ostream_iterator<int>(std::cout," "));
std::cout << std::endl;
// test with vector
std::vector<int> v;
for(int i=0; i<10; i++)
v.push_back(i);
std::cout << "size of v before call to remove " << v.size() << std::endl;
std::vector<int>::iterator p = std::remove(v.begin(), v.end(), 5);
std::cout << "size of v after call to remove " << v.size() << std::endl;
v.erase(p);
std::cout << "size of v after erase " << v.size() << std::endl;
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout," "));
std::cout << std::endl;
}
if (false) {
int buf[10] = { 2, 5, 3, 5, 4, 5, 6, 6, 5, 5 };
std::copy(buf, buf+10, std::ostream_iterator<int>(std::cout," "));
std::cout << std::endl;
std::replace(buf, buf+10, 5, 0);
std::copy(buf, buf+10, std::ostream_iterator<int>(std::cout," "));
std::cout << std::endl;
}
if (false) {
// The reverse algorithm reverses the order of the items in a given range.
int buf[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
std::cout << "Before reverse ";
std::copy(buf, buf+10, std::ostream_iterator<int>(std::cout," "));
std::cout << std::endl;
std::reverse(buf, buf+10);
std::cout << "After reverse ";
std::copy(buf, buf+10, std::ostream_iterator<int>(std::cout," "));
std::cout << std::endl;
}
if (false) {
int buf[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
std::cout << "Before rotate ";
std::copy(buf, buf+10, std::ostream_iterator<int>(std::cout," "));
std::cout << std::endl;
std::rotate(buf, buf+4, buf+10);
std::cout << "After rotate ";
std::copy(buf, buf+10, std::ostream_iterator<int>(std::cout," "));
std::cout << std::endl;
}
if (false) {
// The swap function swaps to values
std::string first="Hello";
std::string second="World";
std::cout << first << " " << second << std::endl;
std::swap(first,second);
std::cout << first << " " << second << std::endl;
// The swap_ranges function swaps ranges.
std::list<std::string> ll;
ll.push_front("Homer");
ll.push_front("Marge");
ll.push_front("Bart");
std::vector<std::string> vec;
vec.push_back("Moe");
vec.push_back("Wiggum");
vec.push_back("Burns");
std::swap_ranges(ll.begin(),ll.end(),vec.begin());
std::cout << "List values ";
std::for_each(ll.begin(),ll.end(),Print<std::string>(" "));
std::cout << std::endl;
std::cout << "Vector values ";
std::for_each(vec.begin(),vec.end(),Print<std::string>(" "));
std::cout << std::endl;
}
if (false) {
std::vector<std::string> v(3);
std::list<std::string> ll;
ll.push_front("Homer");
ll.push_front("Marge");
ll.push_front("Bart");
std::transform(ll.begin(),ll.end(),v.begin(),Upper<std::string>());
std::cout << "Uppercase names ";
std::for_each(v.begin(),v.end(),Print<std::string>(" "));
std::cout << std::endl;
std::list<std::string> ll2;
ll2.push_front("Simpson");
ll2.push_front("Simpson");
ll2.push_front("Simpson");
std::transform(ll.begin(),ll.end(),ll2.begin(),v.begin(),Concat<std::string>(" "));
std::cout << "Concat'ed names ";
std::for_each(v.begin(),v.end(),Print<std::string>("\n"));
std::cout << std::endl;
int a[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int b[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int c[10];
std::transform(a,a+10,b,c,std::plus<int>());
std::for_each(c,c+10,Print<int>(" "));
std::cout << std::endl;
std::transform(a,a+10,c,std::bind2nd(std::plus<int>(),10));
std::for_each(c,c+10,Print<int>(" "));
std::cout << std::endl;
}
if (false) {
int tab[10] = { 1, 2, 2, 3, 4, 4, 5, 6, 6, 7 };
int* res = std::unique(tab, tab+10);
std::cout << "Unique items ";
std::for_each(tab,res,Print<int>(" "));
std::cout << std::endl;
// Binary predicate function version.
int tab2[10] = { 1, 2, 20, 3, 4, 40, 5, 6, 60, 7 };
res = std::unique(tab2, tab2+10, TheSame<int>());
std::cout << "Unique items ";
std::for_each(tab2,res,Print<int>(" "));
std::cout << std::endl;
int tab3[10] = { 1, 2, 20, 3, 4, 40, 5, 6, 60, 7 };
res = std::unique(tab3, tab3+10);
std::cout << "Unique items ";
std::for_each(tab3,res,Print<int>(" "));
std::cout << std::endl;
}
if (false) {
int tab[10];
// initialize
std::generate(tab,tab+10,Rand<int>());
std::cout << "Unsorted sequence ";
std::for_each(tab,tab+10,Print<int>(" "));
std::cout << std::endl;
std::sort(tab,tab+10,std::less<int>());
std::cout << "Sorted ascending order sequence ";
std::for_each(tab,tab+10,Print<int>(" "));
std::cout << std::endl;
// initialize
std::generate(tab,tab+10,Rand<int>());
std::cout << "Unsorted sequence ";
std::for_each(tab,tab+10,Print<int>(" "));
std::cout << std::endl;
std::sort(tab,tab+10,std::greater<int>());
std::cout << "Sorted descending order sequence ";
std::for_each(tab,tab+10,Print<int>(" "));
std::cout << std::endl;
// customize the sort criteria
std::vector<Employee*> v;
v.push_back( new Employee("EEE", "Dept AAA", "Id100") );
v.push_back( new Employee("CCC", "Dept BBB", "Id500") );
v.push_back( new Employee("BBB", "Dept DDD", "Id300") );
v.push_back( new Employee("FFF", "Dept CCC", "Id600") );
v.push_back( new Employee("AAA", "Dept CCC", "Id200") );
v.push_back( new Employee("DDD", "Dept AAA", "Id000") );
std::cout << "Unsorted employees " << std::endl;
std::for_each(v.begin(),v.end(),Print<Employee*>("\n"));
std::cout << std::endl;
// sort by name
std::sort(v.begin(),v.end(),SortByName());
std::cout << "Sorted employees by name " << std::endl;
std::for_each(v.begin(),v.end(),Print<Employee*>("\n"));
std::cout << std::endl;
// sort by id
std::sort(v.begin(),v.end(),SortById());
std::cout << "Sorted employees by id " << std::endl;
std::for_each(v.begin(),v.end(),Print<Employee*>("\n"));
std::cout << std::endl;
// sort by dept
std::sort(v.begin(),v.end(),SortByDept());
std::cout << "Sorted employees by dept " << std::endl;
std::for_each(v.begin(),v.end(),Print<Employee*>("\n"));
std::cout << std::endl;
// sort by name, the by dept
std::sort(v.begin(),v.end(),SortByName());
std::stable_sort(v.begin(),v.end(),SortByDept());
std::cout << "Sorted employees by name then stable sorted by dept " << std::endl;
std::for_each(v.begin(),v.end(),Print<Employee*>("\n"));
std::cout << std::endl;
// initialize
std::generate(tab,tab+10,Rand<int>());
std::cout << "Unsorted sequence " << std::endl;
std::for_each(tab,tab+10,Print<int>(" "));
std::cout << std::endl;
std::partial_sort(tab,tab+3,tab+10,std::greater<int>());
std::cout << "Partially sorted (top 3) in descending order sequence " << std::endl;
std::for_each(tab,tab+10,Print<int>(" "));
std::cout << std::endl;
double salaries[10] = { 22000.0, 28500.00, 17500.00, 22000.0, 54000.00,
77500.00, 18400.00, 100000.00, 23000.0, 50000.0 };
std::nth_element(salaries, salaries+2, salaries+10, std::less<double>());
std::cout << "The 3rd top salary is " << salaries[2] << std::endl;
std::cout << std::endl;
}
if (false) {
std::vector<int> keys;
for (int i=0; i<100; i++)
keys.push_back(i);
std::sort(keys.begin(), keys.end(), std::less<int>());
// is item 55 in the sequence?
bool isThere = std::binary_search(keys.begin(), keys.end(), int(55));
if (isThere)
std::cout << "The key 55 is in the sequence" << std::endl;
else
std::cout << "The key 55 is not in the sequence" << std::endl;
// is item 555 in the sequence?
isThere = std::binary_search(keys.begin(), keys.end(), int(555));
if (isThere)
std::cout << "The key 555 is in the sequence" << std::endl;
else
std::cout << "The key 555 is not in the sequence" << std::endl;
}
if (false) {
std::list<float> ll;
ll.push_front(70.0f);
ll.push_front(60.0f);
ll.push_front(50.0f);
ll.push_front(30.0f);
ll.push_front(20.0f);
ll.push_front(10.0f);
std::list<float>::iterator iter =
std::lower_bound(ll.begin(), ll.end(), 40.0f);
ll.insert(iter, 40.0f);
std::for_each(ll.begin(), ll.end(), Print<float>(" "));
std::cout << std::endl;
std::list<float> ll2;
ll2.push_front(70.0f);
ll2.push_front(60.0f);
ll2.push_front(50.0f);
ll2.push_front(30.0f);
ll2.push_front(20.0f);
ll2.push_front(10.0f);
std::list<float>::iterator iter2 =
std::upper_bound(ll2.begin(), ll2.end(), 40.0f);
ll2.insert(iter2, 40.0f);
std::for_each(ll2.begin(), ll2.end(), Print<float>(" "));
std::cout << std::endl;
std::list<float> ll3;
ll3.push_front(70.0f);
ll3.push_front(60.0f);
ll3.push_front(50.0f);
ll3.push_front(30.0f);
ll3.push_front(20.0f);
ll3.push_front(10.0f);
std::pair< std::list<float>::iterator, std::list<float>::iterator > res =
std::equal_range(ll3.begin(), ll3.end(), 40.0f);
std::cout << *res.first << " " << *res.second << std::endl;
std::cout << std::endl;
}
if (false) {
int tab1[5] = { 10, 20, 30, 40, 50 };
int tab2[5] = { 5, 15, 25, 35, 45 };
int tab3[10];
std::merge(tab1,tab1+5,tab2,tab2+5,tab3);
std::cout << "The merged list ";
std::for_each(tab3,tab3+10,Print<int>(" "));
std::cout << std::endl;
int tab4[10] = { 10, 20, 30, 40, 50, 5, 15, 25, 35, 45 };
std::inplace_merge(tab4,tab4+5,tab4+10);
std::cout << "The in-place merged list ";
std::for_each(tab4,tab4+10,Print<int>(" "));
std::cout << std::endl;
}
if (false) {
// The set algorithms allows set operations on sorted sequences.
int tab1[5] = { 10, 20, 30, 40, 50 };
int tab2[5] = { 10, 25, 30, 45, 50 };
int tab3[10];
// union
int* p = std::set_union(tab1,tab1+5,tab2,tab2+5,tab3);
std::cout << "Union ";
std::for_each(tab3,p,Print<int>(" "));
std::cout << std::endl;
// intersection
p = std::set_intersection(tab1,tab1+5,tab2,tab2+5,tab3);
std::cout << "Intersection ";
std::for_each(tab3,p,Print<int>(" "));
std::cout << std::endl;
// difference
p = std::set_difference(tab1,tab1+5,tab2,tab2+5,tab3);
std::cout << "Difference ";
std::for_each(tab3,p,Print<int>(" "));
std::cout << std::endl;
// symmetric difference
p = std::set_symmetric_difference(tab1,tab1+5,tab2,tab2+5,tab3);
std::cout << "Symmetric difference ";
std::for_each(tab3,p,Print<int>(" "));
std::cout << std::endl;
int tab4[3] = { 20, 30, 40 };
bool res = std::includes(tab1,tab1+5,tab4,tab4+3);
if (res)
std::cout << "Located the sequence in tab4 within tab1" << std::endl;
else
std::cout << "The sequence in tab4 is not located within tab1" << std::endl;
}
if (true) {
int tab[10] = { 2, 4, 6, 1, 3, 5, 8, 7, 9, 100 };
int* p = std::min_element(tab,tab+10);
std::cout << "Min value = " << *p << std::endl;
p = std::max_element(tab,tab+10);
std::cout << "Max value = " << *p << std::endl;
}
return 0;
}