Difference between revisions of "CPP/STL/Vector"

From ProgrammingExamples
< CPP
Jump to: navigation, search
(Created page with '==Vector.cpp== <source lang="cpp"> #include <iostream> #include <vector> #include <cstdlib> #include <algorithm> #include "TestClass.h" double RandomDouble(); void TestVector(…')
 
 
(One intermediate revision by the same user not shown)
Line 6: Line 6:
  
 
#include <algorithm>
 
#include <algorithm>
 
#include "TestClass.h"
 
  
 
double RandomDouble();
 
double RandomDouble();
Line 13: Line 11:
 
void RemoveElement();
 
void RemoveElement();
 
void TestResize();
 
void TestResize();
void TestInsert();
+
void TestInsert(); // append
 
void TestCopy();
 
void TestCopy();
 
void TestFind();
 
void TestFind();
 
void ArrayStyleInit();
 
void ArrayStyleInit();
  
int main(int argc, char* argv[])
+
int main(int, char*[])
 
{
 
{
 
   //TestVector();
 
   //TestVector();
Line 179: Line 177:
 
  std::vector<double> c;
 
  std::vector<double> c;
 
   
 
   
 +
c.insert(c.end(), a.begin(), a.end());
 
  c.insert(c.end(), b.begin(), b.end());
 
  c.insert(c.end(), b.begin(), b.end());
 
   
 
   
Line 190: Line 189:
 
}
 
}
  
 +
</source>
 +
 +
==CMakeLists.txt==
 +
<source lang="cmake">
 +
cmake_minimum_required(VERSION 2.6)
 +
 +
Project(Vector)
 +
 +
ADD_EXECUTABLE(Vector Vector.cpp)
 
</source>
 
</source>

Latest revision as of 15:22, 25 February 2011

Vector.cpp

#include <iostream>
#include <vector>
#include <cstdlib>
 
#include <algorithm>
 
double RandomDouble();
void TestVector();
void RemoveElement();
void TestResize();
void TestInsert(); // append
void TestCopy();
void TestFind();
void ArrayStyleInit();
 
int main(int, char*[])
{
  //TestVector();
  //RemoveElement();
  //TestResize();
  //TestInsert();
  //TestCopy();
  //TestFind();
  ArrayStyleInit();
 
  return 0;
}
 
void ArrayStyleInit()
{
  std::vector<int> vec({1,2,6,7,8,89}); //need -std=c++0x
}
 
void TestFind()
{
  std::vector<unsigned int> V(10);
 
  for(unsigned int i = 0; i < 10; i++)
  {
    V[i] = 10*i;
    std::cout << V[i]   << " ";
  }
 
  std::cout << std::endl;
 
  std::vector<unsigned int>::iterator it = find(V.begin(), V.end(), 70);
 
  if(it == V.end())
  {
      std::cout << "Could not find 70 in the vector"  << std::endl;
  }
  else
  {
    std::cout << "The number 70 is located at index " << it - V.begin() << std::endl;
  }
}
 
void TestCopy()
{
  std::vector<double> V(10);
 
  for(unsigned int i = 0; i < 10; i++)
  {
    V[i] = RandomDouble();
    std::cout << V[i]   << " ";
  }
 
  std::vector<double> B(V.size());
 
  std::copy(V.begin(), V.end(), B.begin());
 
  std::cout << std::endl;
 
  for(unsigned int i = 0; i < 10; i++)
  {
    std::cout << B[i]   << " ";
  }
}
 
double RandomDouble()
{
	//produce a random double between 0 and 1
	return drand48();
}
 
void TestResize()
{
	std::vector<double> V(10);
 
	for(unsigned int i = 0; i < 10; i++)
    {
		V[i] = RandomDouble();
    }
 
	for(unsigned int i = 0; i < 10; i++)
	{
		std::cout << V[i] << " ";
	}
	std::cout << "Size: " << V.size() << std::endl;
 
	V.resize(5);
 
	for(unsigned int i = 0; i < 5; i++)
	{
		std::cout << V[i] << " ";
	}
	std::cout << "Size: " << V.size() << std::endl;
 
}
 
void TestVector()
{
	std::vector<double> V;
 
	std::cout << "Size: " << V.size() << std::endl;
	std::cout << "Empty? " << V.empty() << std::endl;
 
	for(unsigned int i = 0; i < 100; i++)
    {
		V.push_back(RandomDouble());
    }
 
	std::cout << "Size: " << V.size() << std::endl;
	std::cout << "Empty? " << V.empty() << std::endl;
 
	V.clear();
 
	std::cout << "Size: " << V.size() << std::endl;
	std::cout << "Empty? " << V.empty() << std::endl;
}
 
void RemoveElement()
{
	unsigned int num = 10;
	std::vector<unsigned int> V(num);
	for(unsigned int i = 0; i < num; i++)
	{
		V[i] = i;
	}
 
	std::vector<unsigned int>::iterator it = find(V.begin(), V.end(), 7);
	if(it == V.end())
	{
		std::cout << "Could not find 7 in the vector"  << std::endl;
	}
	else
	{
		std::cout  << "Have found 7 in the vector"  << std::endl;
		std::cout << "The number 7 is located at index " << it - V.begin() << std::endl;
	}
 
 
	std::vector<unsigned int>::iterator it2 = find(V.begin(), V.end(), 15);
	if(it2 == V.end())                 
    {
		std::cout  << "Could not find 15 in the vector"  << std::endl;
    }
	else                                
    {
		std::cout  << "Have found 15 in the vector"  << std::endl;
    }
 
}
 
void TestInsert()
{
 std::vector <double> a;
 a.push_back(1);
 a.push_back(2);
 
 std::vector <double> b;
 b.push_back(3);
 b.push_back(4);
 
 std::vector<double> c;
 
 c.insert(c.end(), a.begin(), a.end());
 c.insert(c.end(), b.begin(), b.end());
 
 for(unsigned i = 0; i < c.size(); i++)
 {
     std::cout << c[i] << " ";
 }
 
 std::cout << std::endl;
 
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
 
Project(Vector)
 
ADD_EXECUTABLE(Vector Vector.cpp)