Boost tuple and Vector problem

Error creating a function to populate a vector of tuple<int, double

Page 1 of 1

14 Replies - 2345 Views - Last Post: 28 September 2009 - 11:24 AM Rate Topic: -----

#1 LuckyPierre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 23-September 09

Boost tuple and Vector problem

Posted 23 September 2009 - 03:46 PM

Hi everyone!

I'm doing a test to try and improve my understanding of vectors and boost tuples and am having the following problem.

I have simplified the issue down to the code pasted below and am working with:
- MS Visual C++ 2008
- Boost library 1.38

Aim
- I want to create a vector of tuple<int, double> elements and pass it by reference to a method/template to populate it.

At the moment in the populate method I can create the tuple element and insert it into a vector also created in the method, but I cannot insert the tuple into the vector which has been passed to the method by reference.

Notes

- I cannot change any of the code in main(), I can only write code in the populateData method.
- Not other fancy libraries etc

Error

- The error occurs in the tuple_basic.hpp and I have included it below (cut the length of it but important info is there)

Any help or info would be appreciated!!

Thanks!!!

Code Start===============================================================

#include "stdafx.h"
#include <vector>
#include <iostream>
#include <string>
#include <sstream>
#include <numeric>


#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_io.hpp>


template <typename CONTAINER>
void populateData(CONTAINER& container, size_t count)
{

	typedef boost::tuple<int, double> row1_t;

	std::vector<row1_t> myVect;

	for(size_t i=0; i<count; i++)
	{
		intTuple tempTuple((int(i)));
		myVect.push_back(tempTuple);
	
		row1_t myTuple((int(i)), (double(i))/10);

		myVect.push_back(myTuple);	//This works!
		container.push_back(myTuple);	//THIS IS THE PROBLEM LINE
	};
};


int main()	
{
	typedef boost::tuple<int, double>	row1_t;
	typedef std::vector<row1_t>						vec1_t;
	vec1_t vec1;
	populateData(vec1, 100);
	TEST_REQUIRE_EQUAL(vec1.size(), 100);
	TEST_CHECK_EQUAL_AS_STR(vec1[1],"(1 0.1)");
	TEST_CHECK_EQUAL_AS_STR(vec1[99],"(99 9.9)");


}


Code End================================================================


Full Error:

1>------ Build started: Project: wintonExpertTest, Configuration: Debug Win32 ------
1>Compiling...
1>wintonExpertTest.cpp
1>c:\program files\boost\boost_1_38\boost\tuple\detail\tuple_basic.hpp(373) : error C2039: 'tail' : is not a member of 'boost::tuples::cons<HT,TT>'
1> with
1> [
1> HT=double,
1> TT=boost::tuples::detail::map_tuple_to_cons<boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type>::type
1> ]
1> c:\program files\boost\boost_1_38\boost\tuple\detail\tuple_basic.hpp(373) : see reference to function template instantiation 'boost::tuples::cons<HT,TT>::cons<T0,boost::tuples::detail::map_tuple_to_cons<boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type>::type>(const boost::tuples::cons<HT,boost::tuples::detail::map_tuple_to_cons<boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type>::type> &)' being compiled
1> with
1> [
1> HT=double,
1> TT=boost::tuples::cons<double,boost::tuples::detail::map_tuple_to_cons<boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type>::type>,
1> T0=double
1> ]

Is This A Good Question/Topic? 0
  • +

Replies To: Boost tuple and Vector problem

#2 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: Boost tuple and Vector problem

Posted 23 September 2009 - 04:06 PM

This erros 1>c:\program files\boost\boost_1_38\boost\tuple\detail\tuple_basic.hpp(373) : error C2039: 'tail' : is not a member of 'boost::tuples::cons<HT,TT>'
Is not on this segment of the code. Can you show the tuple_basic.hpp?
And it might be a big problem for the rest of the code ;)
Was This Post Helpful? 0
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Boost tuple and Vector problem

Posted 23 September 2009 - 04:23 PM

well... you code does not work for me but for different reasons.

#1 you did not include the boost namespace (or prefix any of the boost classes with boost::

#2 these macros are totally undefined (unless you have them defined somewhere: TEST_REQUIRE_EQUAL, TEST_CHECK_EQUAL_AS_STR

so I commented out those lines...

#3 intTuple is undefined

you duplicate your typedefs of row1_t and vec1_t (not really necessary)

... What are you trying to do? Whatever it is, it looks like you are trying a very complicated route...
Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Boost tuple and Vector problem

Posted 23 September 2009 - 04:35 PM

Never mind... after making all changes I still came up with the same error:

tuple_basic.hpp 373: 'tail' is not a member of 'cons<int>' in function cons<int,cons<double> >::cons(const cons<int> &)
Was This Post Helpful? 0
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Boost tuple and Vector problem

Posted 23 September 2009 - 04:43 PM

Though trial and error (commenting out code) I found that the line causing the error for me is this:
myVect.push_back(tempTuple);

Try removing that line and see if your code compiles...
Was This Post Helpful? 0
  • +
  • -

#6 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Boost tuple and Vector problem

Posted 23 September 2009 - 04:49 PM

So my code is not the same as yours as I have to modify it slightly but here is my version is this:
#include <vector>
#include <iostream>
#include <string>
#include <sstream>
#include <numeric>


#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_io.hpp>
//using namespace ::boost;

typedef boost::tuple<int, double> row1_t;
typedef boost::tuple<int> intTuple;
typedef std::vector<row1_t> vec1_t;

template <typename CONTAINER>
void populateData(CONTAINER& container, size_t count)
{

   std::vector<row1_t> myVect;

	for(size_t i=0; i<count; i++)
	{
		intTuple tempTuple(i);
		
		//myVect.push_back(tempTuple); //tempTuple is of wrong type it is a boost::tuple<int> not a boost::tuple<int, double>
   
		row1_t myTuple(1, 3.14);

		myVect.push_back(myTuple);	//This works!
		container.push_back(myTuple);	//THIS IS THE PROBLEM LINE
	};
};


int main()	
{
	vec1_t vec1;
	populateData(vec1, 100);
	//TEST_REQUIRE_EQUAL(vec1.size(), 100);
	//TEST_CHECK_EQUAL_AS_STR(vec1[1],"(1 0.1)");
	//TEST_CHECK_EQUAL_AS_STR(vec1[99],"(99 9.9)");
	//row1_t testing(1, 3.14159);
	return 0;

}


And the problem in mine is that they types don't match for the item being pushed into the vector and the vector's type.
Was This Post Helpful? 0
  • +
  • -

#7 LuckyPierre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 23-September 09

Re: Boost tuple and Vector problem

Posted 23 September 2009 - 04:50 PM

Thanks for the replies and appologies for the slightly dodgy code I posted.

Perhaps this would be easier if I show you the whole problem, I've attached the .cpp file (renamed to .txt) with all of the given code in it.

My code may appear a bit long winded as I'm a little new to using tuples and vectors so any hints or tips would be great.

Hope the attached file helps and if there are any questions please let me know!

code is also here if you don't want the file:

#include <vector>
#include <iostream>
#include <string>
#include <sstream>
#include <numeric>

#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_io.hpp>



static bool testFailure(false);
static size_t testCount(0);



#define TEST_REQUIRE_EQUAL(a, b)													\
{																					\
	++testCount;																	\
	if (a != b) {																	\
		std::cout << "ERROR: TEST ABORTING (" #a " != " #b ")" << std::endl;		\
		exit(1);																	\
	}																				\
}

#define TEST_CHECK_EQUAL(a, b)														\
{																					\
	++testCount;																	\
	if (a != b) {																	\
		std::cout << "ERROR: TEST FAILURE (" #a " != " #b ")" << std::endl;			\
		testFailure=true;															\
	}																				\
}

#define TEST_CHECK_EQUAL_AS_STR(a, b)												\
{																					\
	++testCount;																	\
	std::stringstream lhs, rhs;														\
	lhs << a;																		\
	rhs << b;																		\
	if (lhs.str() != rhs.str()) {													\
		std::cout << "ERROR: TEST FAILURE ("										\
			<< lhs.str()															\
			<< " != "																\
			<< rhs.str()															\
			<< ")"																	\
			<< std::endl															\
	;																			\
		testFailure=true;															\
	}																				\
}





template <typename CONTAINER>
void populateData(CONTAINER& container, size_t count)
{
}



template<typename ITERATOR_TYPE>
typename ITERATOR_TYPE::value_type sum(const ITERATOR_TYPE& first, const ITERATOR_TYPE& last)
{
	typedef typename ITERATOR_TYPE::value_type value_type;
	value_type count(static_cast<value_type>(std::distance(first,last)));
	if(count==0)
		throw std::exception("Cannot calculate the sum of empty series.");
	return std::accumulate(first, last, value_type(), std::plus<value_type>());
}


template<typename ITERATOR_TYPE>
typename ITERATOR_TYPE::value_type mean(const ITERATOR_TYPE& first, const ITERATOR_TYPE& last)
{
	typedef typename ITERATOR_TYPE::value_type value_type;
	value_type count(static_cast<value_type>(std::distance(first,last)));
	return sum(first, last)/count;
}

template<typename CONTAINER> 
typename CONTAINER::value_type mean(const CONTAINER& container)							
{
	return mean(container.begin(), container.end());
}


int main()	
{
	// challenge 1 - pass the following tests
	typedef boost::tuple<int, double>				row1_t;
	typedef std::vector<row1_t>						vec1_t;
	vec1_t vec1;
	populateData(vec1, 100);
	TEST_REQUIRE_EQUAL(vec1.size(), 100);
	TEST_CHECK_EQUAL_AS_STR(vec1[1],"(1 0.1)");
	TEST_CHECK_EQUAL_AS_STR(vec1[99],"(99 9.9)");


	typedef boost::tuple<int, double, double>		row2_t;
	typedef std::vector<row2_t>						vec2_t;
	vec2_t vec2;
	populateData(vec2, 100);
	TEST_REQUIRE_EQUAL(vec2.size(), 100);
	TEST_CHECK_EQUAL_AS_STR(vec2[1],"(1 0.1 0.01)");
	TEST_CHECK_EQUAL_AS_STR(vec2[99],"(99 9.9 0.99)");


	// challenge 2 - create a specialisation for strings
	typedef boost::tuple<int, double, std::string>	row3_t;
	typedef std::vector<row3_t>						vec3_t;
	vec3_t vec3;
	populateData(vec3, 100);
	TEST_REQUIRE_EQUAL(vec3.size(), 100);
	TEST_CHECK_EQUAL_AS_STR(vec3[1],"(1 0.1 1/100)");
	TEST_CHECK_EQUAL_AS_STR(vec3[99],"(99 9.9 99/100)");

	// challenge 3 - create a generalised view on the series - ideal solution should not copy data
	//TEST_CHECK_EQUAL_AS_STR( mean(columnViewOfTuple<0>(vec1)), 49);
	//TEST_CHECK_EQUAL_AS_STR( mean(columnViewOfTuple<1>(vec1)), 4.95);

	//TEST_CHECK_EQUAL_AS_STR( mean(columnViewOfTuple<0>(vec2)), 49);
	//TEST_CHECK_EQUAL_AS_STR( mean(columnViewOfTuple<1>(vec2)), 4.95);
	//TEST_CHECK_EQUAL_AS_STR( mean(columnViewOfTuple<2>(vec2)), 0.495);

	//TEST_CHECK_EQUAL_AS_STR( mean(columnViewOfTuple<0>(vec3)), 49);
	//TEST_CHECK_EQUAL_AS_STR( mean(columnViewOfTuple<1>(vec3)), 4.95);

	TEST_CHECK_EQUAL_AS_STR(testCount, 17);
	if (!testFailure) {
		std::cout << "Congratulations!\n";
	}

}




Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#8 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Boost tuple and Vector problem

Posted 23 September 2009 - 05:11 PM

try this:
template <typename TUPLE_CONTAINER, typename CONTAINED_TUPLE>
void populateData(TUPLE_CONTAINER& container, size_t count)
{
	for(size_t i =0; i< count; i++) {
		CONTAINED_TUPLE myTuple(i, double(i)/10);
		container.push_back(myTuple);
	}
}


then inside of your main():
populateData<vec1_t,row1_t>(vec1, 100);
Was This Post Helpful? 0
  • +
  • -

#9 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Boost tuple and Vector problem

Posted 23 September 2009 - 05:19 PM

LOL... well its not a general solution since the tuples get larger... it would take some fancy meta-programming to get the number of arguments to the constructor right... boost has a number of preprocessor functions that can do that for you -- I don't think there is a template solution to that.
Was This Post Helpful? 0
  • +
  • -

#10 LuckyPierre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 23-September 09

Re: Boost tuple and Vector problem

Posted 23 September 2009 - 05:26 PM

thanks for the reply...

I think the idea of the test is to not change the code in main(), if you look lower down at the second part then populateData is called again and so wouldn't passing more data to the template/function be sort of cheating?

Also I think you were just putting in what I did but won't

double(i)/10


if 'i' is 7 give 0.69999999 rather than the required 0.7? (Ican probably figure this part out with more time, I assume you were just doing what I tried to do at first...)

Also any idea how I should approach the second part of the problem (still in challenge 1) where a tuple with three elements is now used? Not sure how to make the template handle all these types...

thanks again for your help :)
Was This Post Helpful? 0
  • +
  • -

#11 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Boost tuple and Vector problem

Posted 23 September 2009 - 05:47 PM

Its a work in progress. :)

This might help you out some:
#include <iostream>

#include <boost/preprocessor/repetition/enum_params.hpp>
#include <boost/preprocessor/repetition/enum.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_io.hpp>


#define TEXT1(z, n, class) class(n+1)
#define TEXT2(z, n, arg) arg


int main() {
	boost::tuple<int, BOOST_PP_ENUM(4, TEXT2, double) > mine(0,
		BOOST_PP_ENUM(4, TEXT1, double)
	);
	std::cout << mine;
	return 0;
}


It shows how you can begin to define those types...

Though if this is a challenge and they have given you the function signature than you are right, your not supposed to add extra arguments... I am not familiar with boost::tuple to see how you can resolve the type of the contained tuple, but I suppose it may be possible.

I would suggest reading over the tuple documentation and re-reading the rules of the challenge.
Was This Post Helpful? 0
  • +
  • -

#12 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Boost tuple and Vector problem

Posted 23 September 2009 - 05:52 PM

same thing but idea extended ever so slightly
#include <iostream>

#include <boost/preprocessor/repetition/enum_params.hpp>
#include <boost/preprocessor/repetition/enum.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_io.hpp>


#define TEXT1(z, n, class) class(n+1)
#define TEXT2(z, n, arg) arg

#define MK_TUPLE(name, size) boost::tuple<int, BOOST_PP_ENUM(size, TEXT2, double) > name(0, \
		BOOST_PP_ENUM(size, TEXT1, double) \
	)


int main() {
	MK_TUPLE(one, 2);
	MK_TUPLE(two, 3);
	MK_TUPLE(three, 4);
	MK_TUPLE(four, 5);
	MK_TUPLE(five, 6);
	MK_TUPLE(six, 7);
	MK_TUPLE(seven, 8);
	
	std::cout << one << std::endl
			  << two << std::endl
			  << three << std::endl
			  << four << std::endl
			  << five << std::endl
			  << six << std::endl
			  << seven << std::endl;
	return 0;
}

Was This Post Helpful? 0
  • +
  • -

#13 LuckyPierre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 23-September 09

Re: Boost tuple and Vector problem

Posted 23 September 2009 - 05:57 PM

I'm in the uk so will have to look at this again tomorrow morning,it's 2am...


Thanks for the reply though, I'll have a look at it and more of a read around tuples and then most probably come back with many more questions :D

Cheers for your help!
Was This Post Helpful? 0
  • +
  • -

#14 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Boost tuple and Vector problem

Posted 23 September 2009 - 10:48 PM

The following will pass the challenge 1 tests...
inline void set_to_val_over10(const boost::tuples::null_type& x, double v) {};

template <class H, class T>
inline void set_to_val_over10(boost::tuples::cons<H, T>& x, double v) { x.get_head() = v; set_to_val_over10(x.get_tail(), (v/10.0)); }


template <typename CONTAINER>
void populateData(CONTAINER& container, size_t count)
{

	for(size_t i =0; i< count; i++) {
		typename CONTAINER::value_type myTuple(i);
		set_to_val_over10(myTuple.get_tail(), double(i)/10.0);
		container.push_back(myTuple);
		//std::cout << "generated tuple: " << myTuple << std::endl;
	}
	
}


I had a sudden brain wave or recalling that you can get the type of a vector using std::vector::value_type -- which allows you to allocate a tuple of the correct type... Then on the boost::tuples documentation here they have an example function called set_to_zero() and with a little modification I created the set_to_val_over10() function that allows the tuples to be initialized.

You SHOULD be able to make a special case for the strings for the second challenge.

good luck
Was This Post Helpful? 0
  • +
  • -

#15 LuckyPierre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 23-September 09

Re: Boost tuple and Vector problem

Posted 28 September 2009 - 11:24 AM

Firstly, thanks for everyone's help so far! So here's my code now.

#include "stdafx.h"
#include <vector>
#include <iostream>
#include <string>
#include <sstream>
#include <numeric>

#include <boost/lexical_cast.hpp>	//MR Added to cast from double to string

#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_io.hpp>


static bool testFailure(false);
static size_t testCount(0);


#define TEST_REQUIRE_EQUAL(a, b)													\
{																					\
	++testCount;																	\
	if (a != b) {																	\
		std::cout << "ERROR: TEST ABORTING (" #a " != " #b ")" << std::endl;		\
		exit(1);																	\
	}																				\
}

#define TEST_CHECK_EQUAL(a, b)														\
{																					\
	++testCount;																	\
	if (a != b) {																	\
		std::cout << "ERROR: TEST FAILURE (" #a " != " #b ")" << std::endl;			\
		testFailure=true;															\
	}																				\
}

#define TEST_CHECK_EQUAL_AS_STR(a, b)												\
{																					\
	++testCount;																	\
	std::stringstream lhs, rhs;														\
	lhs << a;																		\
	rhs << b;																		\
	if (lhs.str() != rhs.str()) {													\
		std::cout << "ERROR: TEST FAILURE ("										\
			<< lhs.str()															\
			<< " != "																\
			<< rhs.str()															\
			<< ")"																	\
			<< std::endl															\
	;																			\
		testFailure=true;															\
	}																				\
}

//Function for challenge 1, for when recursion hits last tuple element
inline void set_to_val_over_ten(const boost::tuples::null_type&, double){};

//Challenge 1 - populates tuple recursively
template<typename H, typename T>
inline void set_to_val_over_ten(boost::tuples::cons<H, T>& x, double V )
{
	x.get_head() = V;
	set_to_val_over_ten(x.get_tail(), V/10);
}

//Challenge 2 - specialisation for strings
template< typename T>
inline void set_to_val_over_ten(boost::tuples::cons<std::string, T>& x, double V )
{

	x.get_head() = boost::lexical_cast<std::string>(V);
	set_to_val_over_ten(x.get_tail(), V/10);
}

//Challenge 1 - Provided function template
template <typename CONTAINER>
void populateData(CONTAINER& container, size_t count)
{
	for(size_t i=0; i<count; i++)
	{
		typename CONTAINER::value_type tupleToAdd(i);
		set_to_val_over_ten(tupleToAdd.get_tail(), double(i)/10.0);
		container.push_back(tupleToAdd);	
	};
}




template<typename ITERATOR_TYPE>
typename ITERATOR_TYPE::value_type sum(const ITERATOR_TYPE& first, const ITERATOR_TYPE& last)
{
	typedef typename ITERATOR_TYPE::value_type value_type;
	value_type count(static_cast<value_type>(std::distance(first,last)));
	if(count==0)
		throw std::exception("Cannot calculate the sum of empty series.");
	return std::accumulate(first, last, value_type(), std::plus<value_type>());
}


template<typename ITERATOR_TYPE>
typename ITERATOR_TYPE::value_type mean(const ITERATOR_TYPE& first, const ITERATOR_TYPE& last)
{
	typedef typename ITERATOR_TYPE::value_type value_type;
	value_type count(static_cast<value_type>(std::distance(first,last)));
	return sum(first, last)/count;
}

template<typename CONTAINER> 
typename CONTAINER::value_type mean(const CONTAINER& container)							
{
	return mean(container.begin(), container.end());
}


int main()	
{
	// challenge 1 - pass the following tests
	typedef boost::tuple<int, double>				row1_t;
	typedef std::vector<row1_t>						vec1_t;
	vec1_t vec1;
	populateData(vec1, 100);
	TEST_REQUIRE_EQUAL(vec1.size(), 100);
	TEST_CHECK_EQUAL_AS_STR(vec1[1],"(1 0.1)");
	TEST_CHECK_EQUAL_AS_STR(vec1[99],"(99 9.9)");


	typedef boost::tuple<int, double, double>		row2_t;
	typedef std::vector<row2_t>						vec2_t;
	vec2_t vec2;
	populateData(vec2, 100);
	TEST_REQUIRE_EQUAL(vec2.size(), 100);
	TEST_CHECK_EQUAL_AS_STR(vec2[1],"(1 0.1 0.01)");
	TEST_CHECK_EQUAL_AS_STR(vec2[99],"(99 9.9 0.99)");


	// challenge 2 - create a specialisation for strings
	typedef boost::tuple<int, double, std::string>	row3_t;
	typedef std::vector<row3_t>						vec3_t;
	vec3_t vec3;
	populateData(vec3, 100);
	TEST_REQUIRE_EQUAL(vec3.size(), 100);
	TEST_CHECK_EQUAL_AS_STR(vec3[1],"(1 0.1 1/100)");
	TEST_CHECK_EQUAL_AS_STR(vec3[99],"(99 9.9 99/100)");

	// challenge 3 - create a generalised view on the series - ideal solution should not copy data
	//TEST_CHECK_EQUAL_AS_STR( mean(columnViewOfTuple<0>(vec1)), 49);
	//TEST_CHECK_EQUAL_AS_STR( mean(columnViewOfTuple<1>(vec1)), 4.95);

	//TEST_CHECK_EQUAL_AS_STR( mean(columnViewOfTuple<0>(vec2)), 49);
	//TEST_CHECK_EQUAL_AS_STR( mean(columnViewOfTuple<1>(vec2)), 4.95);
	//TEST_CHECK_EQUAL_AS_STR( mean(columnViewOfTuple<2>(vec2)), 0.495);

	//TEST_CHECK_EQUAL_AS_STR( mean(columnViewOfTuple<0>(vec3)), 49);
	//TEST_CHECK_EQUAL_AS_STR( mean(columnViewOfTuple<1>(vec3)), 4.95);

	TEST_CHECK_EQUAL_AS_STR(testCount, 17);
	if (!testFailure) {
		std::cout << "Congratulations!\n";
	}

}







The problem I'm now having is when main() calls the following line in challenge 2

TEST_CHECK_EQUAL_AS_STR(vec3[1],"(1 0.1 1/100)");


I've made the mistake of just changing the double to a string instead of a fraction such as "1/100" by using the boost lexical cast.

While I could change the template function to unclude the vector element number currently being iterated
(In the above case this would give the numerator) and also pass in the tuple element number being
iterated through (10^ this number would give the denominator).

I'm fairly sure there must be a more elegant way of doing this?

Any ideas please? :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1