ok irgendwie hat das nicht geklappt... habe aber jetzt folgenden Code zum laufen gebracht...
Code:
#include <string>
#include <iostream>
#include <list>
#include <boost/any.hpp>
#include <stdio.h>
using boost::any_cast;
typedef std::list<boost::any> many;
/*
struct property
{
property();
property(const std::string &, const boost::any &);
std::string name;
boost::any value;
};
typedef std::list<property> properties;
*/
// append_xxxx
void append_int(many & values, int value)
{
boost::any to_append = value;
values.push_back(to_append);
}
void append_string(many & values, const std::string & value)
{
values.push_back(value);
}
void append_char_ptr(many & values, const char * value)
{
values.push_back(value);
}
void append_any(many & values, const boost::any & value)
{
values.push_back(value);
}
void append_nothing(many & values)
{
values.push_back(boost::any());
}
/// IS_xxxxx
bool is_empty(const boost::any & operand)
{
return operand.empty();
}
bool is_int(const boost::any & operand)
{
return operand.type() == typeid(int);
}
bool is_char_ptr(const boost::any & operand)
{
try
{
any_cast<const char *>(operand);
return true;
}
catch(const boost::bad_any_cast &)
{
return false;
}
}
bool is_string(const boost::any & operand)
{
return any_cast<std::string>(&operand);
}
// count_all
void count_all(many & values, std::ostream & out)
{
out << "#empty == "
<< std::count_if(values.begin(), values.end(), is_empty) << std::endl;
out << "#int == "
<< std::count_if(values.begin(), values.end(), is_int) << std::endl;
out << "#const char * == "
<< std::count_if(values.begin(), values.end(), is_char_ptr) << std::endl;
out << "#string == "
<< std::count_if(values.begin(), values.end(), is_string) << std::endl;
}
int main()
{
many values;
append_int(values,3);
append_int(values,1);
append_int(values,4);
append_int(values,5);
append_int(values,6);
append_int(values,8856);
append_any(values,23223);
append_string(values,"test");
append_string(values,"asa");
append_string(values,"tast");
append_string(values,"sd");
append_string(values,"ddsdt");
append_any(values,"tste"); //wird irgendwie nicht mitgezählt
std::cout << std::count_if(values.begin(), values.end(), is_int)<<std::endl; // 7
std::cout << std::count_if(values.begin(), values.end(), is_string)<<std::endl; // 5
return 0;
}
wie es scheint speichert er die werte jeweils in values ab...
mitm Zählen happert es etwas... woran kann das liegen??
die Int Werte zählt er richtig, aber die String Werte nicht...
registriert der die
Code:
append_any(values,"tste");
nicht als string??
wieso macht er es dann bei der Zeile
Code:
append_any(values,23223);
???
nun wollte ich die gesammte Liste auf der Console ausgeben...
nur habe ich keinen Plan wie das geht...
hat jemand entsprechenden Code dafür??
gibt es sowas wie count_if auch für die Ausgabe??
wo ist dieses count_if definiert???
in Boost??
finde leider in der Boost Hilfe nichts zu dem count_if
und in den man Pages auch nicht...
wie kann ich auf einzelne Elemente zugreifen in der Liste??
benötige ich dafür wieder einen Iterator??
wenn ja, wie erzeuge ich den für list<boost::any> ??
und wie sieht dann der code zur ausgabe der Liste aus??
merci schon mal
ufosworld
Lesezeichen