So i'm making a sorting function and i want it to sort my elements depending on the getter i give it.
The problem is my getter is const and thats why my current code doesn't work. If i remove const it works just fine.
struct User {
string name;
string getX() const { return name; } //// doesnt work with this
string getX() { return name; } //// this works fine. but i need my getter to be const
};
template <typename ElemType, typename MemberType>
void _sort(std::vector<ElemType>& vec, MemberType(ElemType::* member)(), bool ascending = true)
{
for (int j = 0; j < vec.size() - 1; ++j)
for (int i = 0; i < vec.size() - j - 1; ++i)
if (((vec[i].*member)() < (vec[i + 1].*member)()) ^ ascending)
swap(vec[i], vec[i + 1]);
}
int main()
{
vector<User> a{ User{"Z"}, User{"E"}, User{"B"}, User{"R"}, User{"A"} };
for (auto e : a)
cout << e.name << " ";
cout << endl;
_sort(a, &User::getX);
for (auto e : a)
cout << e.name << " ";
cout << endl;
return 0;
}
What do i do to make it work with const getter
Just make the parameter const as well:
template <typename ElemType, typename MemberType>
void _sort(std::vector<ElemType>& vec, MemberType(ElemType::* member)() const, bool ascending = true)
________________________________________________________________________^^^^^
Or don't make the parameter that specific:
template <typename ElemType, typename MemberType>
void _sort(std::vector<ElemType>& vec, MemberType member, bool ascending = true)
The former won't work if the getter isn't const, while the latter will work with anything. If you pass the latter a function that cannot be called like (vec[i].*member)(), then the compilation error will be there instead of in the signature of _sort.
Related
How can I factor the following code, so that I can do loop through T = double and T = float? I have read about variadic templates but I
don't understand how to apply it in this case:
int main(int argc, char* argv[])
{
ofstream writeDat;
vector<int> nValues = {26,51,101,201};
for(int i = 0; i< 4; i++){
int N = nValues[i];
typedef float T ;
Matrix<T> a(N,N);
Matrix<T> b(N,3);
Matrix<T> x = Problem2<T>(N);
string sFloat = "2/" + to_string(N) + "Float"+".dat";
writeDat.open(sFloat);
for(int i =1; i<N ; i++)
writeDat << i << " " << x(i,1)<<endl;
writeDat << N <<" "<< x(N,1)<< endl;
writeDat.close();
}
for(int i = 0; i< 4; i++){
int N = nValues[i];
typedef double T ;
Matrix<T> a(N,N);
Matrix<T> b(N,3);
Matrix<T> x = Problem2<T>(N);
string s = "2/" + to_string(N) + "Double"+".dat";
writeDat.open(s);
for(int i =1; i<N ; i++)
writeDat << i << " " << x(i,1)<<endl;
writeDat << N <<" "<< x(N,1)<< endl;
writeDat.close();
}
return 0;
}
Use variadic expansion to call a template function (or variadic lambda) containing your duplicated logic:
#include<fstream>
#include<vector>
// the concept of a type wrapper
template<class T> struct type_wrapper;
// a model of type_wrapper for floats
template<>
struct type_wrapper<float> {
using type = float;
constexpr const char* name() const { return "Float"; }
};
// a model of type_wrapper for doubles
template<>
struct type_wrapper<double> {
using type = double;
constexpr const char* name() const { return "Double"; }
};
// call a template function once for each type wrapper in Ts...
template<class...Ts, class F>
auto for_each_type(F&& f)
{
(f(type_wrapper<Ts>()),...);
}
template<class T>
struct Matrix
{
Matrix(int, int);
T& operator()(int, int);
};
template<class T> Matrix<T> Problem2(int);
int main()
{
auto process = [](auto twrap) {
using T = typename decltype(twrap)::type;
std::ofstream writeDat;
std::vector<int> nValues = {26,51,101,201};
for(int i = 0; i< 4; i++){
int N = nValues[i];
Matrix<T> a(N,N);
Matrix<T> b(N,3);
Matrix<T> x = Problem2<T>(N);
std::string sFloat = "2/" + std::to_string(N) + twrap.name() + ".dat";
writeDat.open(sFloat);
for(int i =1; i<N ; i++)
writeDat << i << " " << x(i,1)<<std::endl;
writeDat << N <<" "<< x(N,1)<< std::endl;
writeDat.close();
}
};
for_each_type<double, float>(process);
}
https://godbolt.org/z/w6g6AC
Note:
You can make for_each_type more portable (i.e. work on c++14) like this:
template<class...Ts, class F>
auto for_each_type(F&& f)
{
#if __cplusplus >= 201703L
(f(type_wrapper<Ts>()),...);
#else
using expand = int[];
expand {
0,
(f(type_wrapper<Ts>()), 0)...
};
#endif
}
I have read about variadic templates but I don't understand how to
apply it in this case:
I believe what you are asking is answered most adequately by #RichardHodges. For reference sake, in case you want to be able to compare doing this the variadic template (since C++11) or fold expression (since C++17) way with doing this the function template only way (not that there's a special reason to do so), then you could use this snippet for example:
#include <vector>
#include <fstream>
using namespace std;
// Undefined struct
template <typename T> struct not_available;
// Non-specialized instantiations are not allowed, by using the undefined struct
template <typename T>
constexpr const char* type_string(){ return not_available<T>{}; }
// Specializing for `float`
template <>
constexpr const char* type_string<float>(){ return "Float"; }
// Specializing for `Double`
template <>
constexpr const char* type_string<double>(){ return "Double"; }
// Your classes
template<class T>
struct Matrix
{
Matrix(int, int);
T& operator()(int, int);
};
template<class T> Matrix<T> Problem2(int);
ofstream writeDat;
vector<int> nValues = {26,51,101,201};
// Your routine
template <typename T>
void func()
{
for(int i = 0; i< 4; i++){
int N = nValues[i];
Matrix<T> a(N,N);
Matrix<T> b(N,3);
Matrix<T> x = Problem2<T>(N);
string s = "2/" + to_string(N) + type_string<T>() +".dat";
writeDat.open(s);
for(int i =1; i<N ; i++)
writeDat << i << " " << x(i,1)<<endl;
writeDat << N <<" "<< x(N,1)<< endl;
writeDat.close();
}
}
int main(int argc, char* argv[])
{
func<float>();
func<double>();
return 0;
}
Note: This snippet tries to stick as much as possible to your original, but the absence of good enough reason I wouldn't necessarily advise using global variables, neither opting for using namespace std instead of referring to names with std::.
I just wrote a simple utility function for std::string. Then I noticed that the function would look exactly the same if the std::string was a std::wstring or a std::u32string. Is it possible to use a template function here? I am not very familiar with templates, and std::string and std::wstring are templates themselves, which might be an issue.
template<class StdStringClass>
inline void removeOuterWhitespace(StdStringClass & strInOut)
{
const unsigned int uiBegin = strInOut.find_first_not_of(" \t\n");
if (uiBegin == StdStringClass::npos)
{
// the whole string is whitespace
strInOut.clear();
return;
}
const unsigned int uiEnd = strInOut.find_last_not_of(" \t\n");
strInOut = strInOut.substr(uiBegin, uiEnd - uiBegin + 1);
}
Is this a proper way to do it? Are there pitfalls with this idea. I am not talking about this function but the general concept of using a templated class StdStringClass and calling the usual std::string functions like find, replace, erase, etc.
Its a good Idea, But I'd build the template on top of std::basic_string rather then general StdStringclass
template<class T>
inline void removeOuterWhitespace(std::basic_string<T>& strInOut)
{
constexpr auto delim[] = {T(' '),T('\t'),T('\n'),T(0)};
const auto uiBegin = strInOut.find_first_not_of(delim);
if (uiBegin == std::basic_string<T>::npos)
{
// the whole string is whitespace
strInOut.clear();
return;
}
const auto uiEnd = strInOut.find_last_not_of(delim);
strInOut = strInOut.substr(uiBegin, uiEnd - uiBegin + 1);
}
I would also ditch the MSDN-style "inout" notation in favro for simpler name like str. programmer will guess themselves that str is the result since it is passed as non-const reference and function returns void.
also, I changed unsigned int to auto. all the standard C++ containers/strings return size_t when returning indexes. size_t might not be unsigned int. auto matches itself to the right return value.
Assuming your template works as expected (haven't checked...sorry), another option would be to wrap the function in class, and control which types of strings classes you'd like the function to be applied to using constructors.
EDIT: added illustrative framework
EDIT2 one that compiles (at least with vs2015) :-)
class StringType1;
class StringTypeN;
class str {
//template function
template<class StdStringClass>
inline void removeOuterWhitespace(StdStringClass & strInOut)
{
//.
//.
//.
}
public:
//constructors
str(StringType1 &s1) { removeOuterWhitespace(s1); }
//.
//.
//.
str(StringTypeN &sN) { removeOuterWhitespace(sN); }
};
int main() {
return 0;
}
EDIT3 Proof of concept
#include <iostream>
class incr {
//template function
template<class incrementor>
inline void removeOuterWhitespace(incrementor & n)
{
n++;
}
public:
//constructors
incr(int &n1) { removeOuterWhitespace(n1); }
incr(double &n1) { removeOuterWhitespace(n1); }
incr(float &n1) { removeOuterWhitespace(n1); }
};
int main() {
int n1 = 1;
double n2 = 2;
float n3 = 3;
std::cout << n1 << "\t" << n2 << "\t" << n3 << std::endl;
auto test1 = incr(n1);
auto test2 = incr(n2);
auto test3 = incr(n3);
//all variables modified
std::cout << "all variables modified by constructing incr" << std::endl;
std::cout << n1 << "\t" << n2 << "\t" << n3 << std::endl;
return 0;
}
What I'm trying to do is change this sort
There is a map < string,int> and it is sorted by frequency
typedef map<string,int> word_Count_List;
word_Count_List word_Count;
struct val_Lessthan : binary_function < pair<string,int>, pair<string,int>, bool > {
bool operator() (const pair<string,int>& x, const pair<string,int>& y) const
{return x.second>y.second;}
} val_lt;
and later
vector<pair<string,int> > wordVector;
copy(word_Count.begin(), word_Count.end(), back_inserter(wordVector));
sort(wordVector.begin(), wordVector.end(), val_lt);
I want to print a list of words with their occurrence(which it does now) but change the compare to int * string.length, instead of just int
This is how i'm printing it at the moment
ofstream file_Out;
file_Out.open(file);
for(unsigned int i=0; i<wordVector.size(); ++i) {
file_Out << wordVector[i].first << " " << wordVector[i].second << "\n";
}
file_Out.close();
}
I'm not too good with structs and overloading, can I change the one above or do I need to do something new?
Is it legal to do something like
{return (x.first.length*x.second)>(y.first.length*y.second);}
How can I create one comparator to compare on different fields. Different fields can have different types (uint or string). Should I use T *?
It is necessary to reduce the code length.
template<typename T>
class ComparatorSelector
{
public:
struct CompareByLabel{
bool operator() ( const T & iRight, const T & iLeft )
{
return iRight->m_label > iLeft->m_label;
}
};
struct CompareByHouseNumber{
bool operator() ( const T & iRight, const T & iLeft )
{
return iRight->m_houseNumber > iLeft->m_houseNumber;
}
};
//...
};
template< class T, class C, typename W >
class SearchIndex
{
public:
SearchIndex() {}
void Build( std::vector< T > iElems, C iComparator, std::ofstream oStream )
{
std::map< T *, size_t> numbersOfElems;
for( class std::vector<T>::iterator it = iElems.begin(); it != iElems.end(); ++it){
m_elems.insert( &(*it));
numbersOfElems[&(*it)] = m_elems.end - it ;
}
oStream << m_elems.size();
for( class std::multiset< T * >::iterator it = m_elems.begin(); it!= m_elems.end(); ++it )
oStream << numbersOfElems[*it];
m_compareMode = iComparator;
}
//....
}
You can use pointers to members to customize your comparator objects. The slower but simpler approach is this:
#include <iostream>
template <typename Type, typename Class>
class comparator
{
Type Class::*d_member;
public:
comparator(Type Class::*member): d_member(member) {}
bool operator()(Class const& object0, Class const& object1) const {
return object0.*(this->d_member) < object1.*(this->d_member);
}
};
template <typename Type, typename Class>
comparator<Type, Class>
make_comparator(Type Class::*member)
{
return comparator<Type, Class>(member);
}
int main()
{
typedef std::pair<int, double> pair;
pair p0(17, 3.14);
pair p1(42, 2.7);
std::cout << std::boolalpha
<< "first: " << make_comparator(&pair::first)(p0, p1) << ' '
<< "second: " << make_comparator(&pair::second)(p0, p1) << ' '
<< '\n';
}
Since this version uses a pointer to member at run-time, it cannot be easily inlined and, thus, isn't as fast as you'd possibly want it to be. The member can also be embedded into the comparator's type making both its use a bit annoying:
template <typename Type, typename Class, Type Class::*Member>
class comparator
{
public:
bool operator()(Class const& object0, Class const& object1) const {
return object0.*Member < object1.*Member;
}
};
int main()
{
typedef std::pair<int, double> pair;
pair p0(17, 3.14);
pair p1(42, 2.7);
std::cout << std::boolalpha
<< "first: " << comparator<int, pair, &pair::first>()(p0, p1) << ' '
<< "second: " << comparator<double, pair, &pair::second>()(p0, p1) << ' '
<< '\n';
}
This is an example of a comparator that uses different fields of different types:
#include <set>
using namespace std;
class House {
public:
string m_label;
int m_houseNumber;
};
class HouseCompare {
public:
bool operator()( const House& a, const House& b)
{
if (a.m_houseNumber>0 && b.m_houseNumber>0)
return a.m_houseNumber < b.m_houseNumber;
else if (a.m_houseNumber>0)
return false;
else if (b.m_houseNumber)
return true;
else
return a.m_label < b.m_label;
}
};
int main(int argc, char *argv[])
{
typedef multiset<House, HouseCompare> Houses;
Houses houses;
House house_data[] = {
{"foo", 1},
{"foo1", 0},
{"foo0", 0},
{"foo", 2}
};
houses.insert (house_data, house_data+sizeof(house_data)/sizeof(House));
for (Houses::iterator i = houses.begin (); i != houses.end (); ++i)
cout << i->m_houseNumber << ": " << i->m_label << endl;
return 0;
}
Output:
0: foo0
0: foo1
1: foo
2: foo
Is it possible to create an STL-like container, or even just an STL-style iterator, for an existing array of POD-type elements?
For example, suppose I have an array of ints. It would be convenient to be able to call some of the STL functions, such as find_if, count_if, or sort directly on this array.
Non-solution: copying the entire array, or even just references to the elements. The goal is to be very memory- and time-saving while hopefully allowing use of other STL algorithms.
You can call many of the STL algorithms directly on a regular C style array - they were designed for this to work. e.g.,:
int ary[100];
// init ...
std::sort(ary, ary+100); // sorts the array
std::find(ary, ary+100, pred); find some element
I think you'll find that most stuff works just as you would expect.
You can use an inline function template so that you don't have to duplicate the array index
template <typename T, int I>
inline T * array_begin (T (&t)[I])
{
return t;
}
template <typename T, int I>
inline T * array_end (T (&t)[I])
{
return t + I;
}
void foo ()
{
int array[100];
std::find (array_begin (array)
, array_end (array)
, 10);
}
All the STL algorithms use iterators.
A pointer is a valid iterator into an array of objects.
N.B.The end iterator must be one element past the end of the array. Hence the data+5 in the following code.
#include <algorithm>
#include <iostream>
#include <iterator>
int main()
{
int data[] = {4,3,7,5,8};
std::sort(data,data+5);
std::copy(data,data+5,std::ostream_iterator<int>(std::cout,"\t"));
}
You can use Boost.Array to create a C++ array type with STL semantics.
using arrays:
int a[100];
for (int i = 0; i < 100; ++i)
a[i] = 0;
using boost.arrays:
boost::array<int,100> a;
for (boost::array<int,100>::iterator i = a.begin(); i != a.end(); ++i)
*i = 0;
Update: With C++11, you can now use std::array.
A pointer is a valid model of an iterator:
struct Bob
{ int val; };
bool operator<(const Bob& lhs, const Bob& rhs)
{ return lhs.val < rhs.val; }
// let's do a reverse sort
bool pred(const Bob& lhs, const Bob& rhs)
{ return lhs.val > rhs.val; }
bool isBobNumberTwo(const Bob& bob) { return bob.val == 2; }
int main()
{
Bob bobs[4]; // ok, so we have 4 bobs!
const size_t size = sizeof(bobs)/sizeof(Bob);
bobs[0].val = 1; bobs[1].val = 4; bobs[2].val = 2; bobs[3].val = 3;
// sort using std::less<Bob> wich uses operator <
std::sort(bobs, bobs + size);
std::cout << bobs[0].val << std::endl;
std::cout << bobs[1].val << std::endl;
std::cout << bobs[2].val << std::endl;
std::cout << bobs[3].val << std::endl;
// sort using pred
std::sort(bobs, bobs + size, pred);
std::cout << bobs[0].val << std::endl;
std::cout << bobs[1].val << std::endl;
std::cout << bobs[2].val << std::endl;
std::cout << bobs[3].val << std::endl;
//Let's find Bob number 2
Bob* bob = std::find_if(bobs, bobs + size, isBobNumberTwo);
if (bob->val == 2)
std::cout << "Ok, found the right one!\n";
else
std::cout << "Whoops!\n";
return 0;
}