Esempio n. 1
0
void TestTransform()
{
  typedef vector<int> Vec;
  Vec test1;
  Vec checked1;
  Vec checked;
  for(int i = 1; i < 5; i++) 
  {
	test1.push_back (i * i);                    // test1: 1 4 9  16 
	checked1.push_back (i * i);
  }

  //vector<int> test2(test1.size());
  //transform(test1.begin(), test1.end(), test2.begin(), [](int i){return ++i;});       // test2: 2 5 10 17

  Vec test2p;
  transform(test1.begin(), test1.end(), back_inserter(test2p), [](int i){return ++i;});       // test2: 2 5 10 17


  cout << "Unary operation, the first is the same: ";
  checked.push_back(2);
  checked.push_back(5);
  checked.push_back(10);
  checked.push_back(17);
  CHECK_RESULTS(IsEqual(test1, checked1));
  cout << "Unary operation, the second has changed: ";
  CHECK_RESULTS(IsEqual(test2p, checked));

  cout << "Binary operation, the first is the same: ";
  checked.clear();
  checked.push_back(3);
  checked.push_back(9);
  checked.push_back(19);
  checked.push_back(33);
  transform(test1.begin(), test1.end(), test2p.begin(), test2p.begin(), [](int i, int j){return i + j;}); // test1: 3 9 19 33 
  CHECK_RESULTS(IsEqual(test1, checked1));   
  cout << "Binary operation, the second has changed: ";
  CHECK_RESULTS(IsEqual(test2p, checked));

  cout << "No elements: ";
  test1.erase(test1.begin(), test1.end());
  checked.erase(checked.begin(), checked.end());
  test2p.erase(test2p.begin(), test2p.end());
  transform(test1.begin(), test1.end(), back_inserter(test2p),[](int i){return ++i;});
  CHECK_RESULTS(IsEqual(test2p, checked));
 }
Esempio n. 2
0
TEST(VectorMapTest, EraseTest)
{
	Vec v;
	ASSERT_TRUE(v.empty());
	ASSERT_EQ(0, v.size());
	ASSERT_FALSE(v.remove(10));
	v.insert(10, 1);
	v.insert(11, 2);
	ASSERT_TRUE(v.end() != v.find(10));
	{
		Pair const& p = *(v.find(10));
		ASSERT_EQ(1, p.second);
	}
	{
		Pair const& p = *(v.erase( v.find(10) ));
		ASSERT_EQ(2, p.second);
	}
}
int main()
{
    Vec<int> ages;
    ages.push_back(31);
    ages.push_back(60);
    ages.push_back(0);
    ages.push_back(36);
    ages.push_back(14);
    ages.push_back(0);
    Vec<int>::size_type n = ages.size();
    ages[n-1] = 7;

    ages.erase(2);
    n = ages.size();

    for (Vec<int>::size_type i = 0; i != n; i++)
        cout << ages[i] << endl;

    return 0;
}