Hi to all you boost gurus out there!
I want to find a certain element in a vector of strings, ignoring case:
#include <iostream>
#include <string>
#include <vector>
#include "boost/algorithm/string.hpp"
#include "boost/bind.hpp"
using std::string;
using std::vector;
bool icmp(const string& str1, const string& str2)
{
return boost::iequals(str1, str2);
}
int main(int argc, char* argv[])
{
vector<string> vec;
vec.push_back("test");
// if (std::find_if(vec.begin(), vec.end(), boost::bind(&boost::iequals<string,string>, "TEST", _1)) != vec.end()) <-- does not compile
if (std::find_if(vec.begin(), vec.end(), boost::bind(&icmp, "TEST", _1)) != vec.end())
std::cout << "found" << std::endl;
return 0;
}
This is working fine so far, but what I would like to know is, if it is possible to get rid of the extra function (icmp()) and invoke iequals (template function) directly (like in the commented line).
Thanks in advance!
Adding the template params and the default locale parameter works on my machine.
if (std::find_if(vec.begin(), vec.end(), boost::bind(&boost::iequals<string,string>, "TEST", _1, std::locale())) != vec.end())
std::cout << "found" << std::endl;
Compiler is VS2010.
I'm sure this is not what you're hoping for, but this appears to be the only fix I can work out (with g++ c++03 mode):
typedef bool (*fptr)(const std::string&, const std::string&);
if (std::find_if(vec.begin(), vec.end(),
boost::bind((fptr) &boost::iequals<string,string>, "TEST", _1)
) != vec.end())
Using boost lambda :)
#include <iostream>
#include <boost/lambda/bind.hpp>
#include <boost/lambda/lambda.hpp>
#include <algorithm>
#include <vector>
using namespace std;
using namespace boost::lambda;
int main()
{
vector<string> vec;
vec.push_back("TEST");
vec.push_back("test");
vector<string>::iterator it;
it = find_if(vec.begin(),vec.end(),_1 == "test");
cout << *it << endl;
return 0;
}
Related
I am trying to pass an array of strings off to a function so that it will sort it along with a Class Template array object of strings which I don't even know what that would look like. But as it is right now. When I use size() inside of main it works just fine but when I use size() inside a function I am typing up it throws out this error.
#include <iostream>
#include <iomanip>
#include <array>
#include <string>
#include <algorithm> // contains sort and binary_search
using namespace std;
void sortArrays(string arr[]);
int main()
{
array<string, 5> hello = {"Hello", "world", "How", "Are", "You"};
sortArrays(&hello[0]);
}
void sortArrays(string array[])
{
string *arrPtr = array;
sort(*arrPtr.begin(), *arrPtr.end());
cout << endl << endl;
}
Like this, std::array it's easy to pass by reference.
#include <algorithm>
#include <array>
#include <iostream>
#include <vector>
#include <string>
void sortArray(std::array<std::string,5>& arr)
{
std::sort(arr.begin(),arr.end());
}
int main()
{
std::array<std::string, 5> hello = { "Hello", "world", "How", "Are", "You" };
sortArray(hello);
for (const auto& str : hello)
{
std::cout << str << " ";
}
std::cout << std::endl;
}
I am relatively new to CPP and have recently stumbled upon std::variant for C++17.
However, I am unable to use the << operator on such type of data.
Considering
#include <iostream>
#include <variant>
#include <string>
using namespace std;
int main() {
variant<int, string> a = "Hello";
cout<<a;
}
I am unable to print the output. Is there any short way of doing this? Thank you so much in advance.
You can use std::visit if you don't want to use std::get.
#include <iostream>
#include <variant>
struct make_string_functor {
std::string operator()(const std::string &x) const { return x; }
std::string operator()(int x) const { return std::to_string(x); }
};
int main() {
const std::variant<int, std::string> v = "hello";
// option 1
std::cout << std::visit(make_string_functor(), v) << "\n";
// option 2
std::visit([](const auto &x) { std::cout << x; }, v);
std::cout << "\n";
}
use std::get
#include <iostream>
#include <variant>
#include <string>
using namespace std;
int main() {
variant<int, string> a = "Hello";
cout << std::get<string>(a);
}
If you want to get automatically, it can't be done without knowing its type. Maybe you can try this.
string s = "Hello";
variant<int, string> a = s;
cout << std::get<decltype(s)>(a);
#include <iostream>
#include <variant>
#include <string>
int main( )
{
std::variant<int, std::string> variant = "Hello";
std::string string_1 = std::get<std::string>( variant ); // get value by type
std::string string_2 = std::get<1>( variant ); // get value by index
std::cout << string_1 << std::endl;
std::cout << string_2 << std::endl;
//may throw exception if index is specified wrong or type
//Throws std::bad_variant_access on errors
//there is also one way to take value std::visit
}
Here is the description link: https://en.cppreference.com/w/cpp/utility/variant
I'm completely new to the range library, so I shouldn't be surprised that this code isn't compiling and I cannot figure out why:
#include <iostream>
#include <algorithm>
#include <fstream>
#include <iterator>
#include <vector>
#include <range/v3/all.hpp>
#include <range/v3/view/all.hpp>
using namespace ranges::v3;
std::ifstream open_file(const std::string &filename) {
return std::ifstream{filename};
}
int count_lines(std::ifstream &in) {
return std::count(std::istreambuf_iterator<char>{in},
std::istreambuf_iterator<char>{}, '\n');
}
std::vector<int>
count_lines_in_files(const std::vector<std::string> &filenames) {
auto a1 = filenames | view::transform(open_file) | view::transform(count_lines);
return a1;
}
int main() {
const std::vector<std::string> files{"listing1_1.cpp",
"listing1_2.cpp",
"listing1_4.cpp",
"listing1_5.cpp"};
const auto result = count_lines_in_files(files);
std::cout << ranges::view::all(result) << '\n';
}
It appears that the complaint is about a1, which the compiler tells me "error: variable has incomplete type 'void'."
Can someone see what I'm doing wrong, or tell me how to properly chain these together if possible?
Thanks in advance!
As noted by Porsche9II, "std::ifstream doesn't have a copy constructor". You can find more on this topic here:
Why are iostreams not copyable?
C++11 introduced a move constructor (6) for std::basic_ifstream, so you could write
auto open_file(const std::string &filename) {
return std::ifstream{filename};
}
auto count_lines(std::ifstream &&in) {
return std::count(std::istreambuf_iterator<char>{in},
std::istreambuf_iterator<char>{}, '\n');
}
Testable HERE.
std::ifstream doesn't have a copy constructor - returning std::ifstream by a function is not a good idea. One possible solution: opening and counting should take place in one function.
I am trying to reverse a string (c++, compiling with g++).
Isn't string considered a container for the algorithm functions?
This is the code:
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string str = "hello";
str.reverse(str.begin(), str.rbegin());
return 0;
}
Thanks
The std::string class template does not have a member function called reverse. There is a std::reverse function located in the <algorithm> header. You probably want to use it in a following manner:
#include <string>
#include <algorithm>
int main() {
std::string str = "hello";
std::reverse(str.begin(), str.end());
}
Note the use of str.end() in place of your str.rbegin(). You can also define a new string and use the string constructor overload that accepts reverse iterators:
#include <string>
int main() {
std::string str = "hello";
std::string reversestr(str.rbegin(), str.rend());
}
std::string has no method reverse. But std::reverse exists:
#include <string>
#include <algorithm>
#include <iostream>
int main()
{
std::string str = "hello";
std::reverse(str.begin(), str.end());
std::cout << str << "\n"; // prints "olleh"
}
I am currently trying to write a list of pairs. my code is :
#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>
#include <list>
using namespace std;
list<pair<string,char>> listPair;
list<pair<string,char>>::iterator it;
void printStars(list<pair<string,char>> listPair)
{
for (it=listPair.begin(); it != listPair.end(); it++)
cout << it->first <<" ";
cout << endl;
}
int main()
{
pair<string,char> mypair;
listPair.push_back(make_pair("bib",'a'));
listPair.push_back(make_pair("bob",'b'));
for_each(listPair.begin(), listPair.end(), printStars);
return 0;
}
Compilation fails with:
error C2664: 'void (std::list<_Ty>)' : cannot convert parameter 1 from 'std::pair<_Ty1,_Ty2>' to 'std::list<_Ty>'
Can you please help me detect where exactly is the problem?
The functor you pass to std::for_each is expected to accept an element of the range you pass into std::for_each. Your last has pair<string,char> elements, so your functor should have a signature like: void printStars(const pair<string,char>& elem).
In addition, to pass a plain function to std::for_each you need to use std::ref or (on an old compiler) std::ptr_fun.
#include <iostream>
#include <algorithm>
#include <list>
#include <string> // missing include
#include <utility>
#include <functional>
using namespace std;
typedef list< pair<string,char> > list_t;
list_t listPair;
void printStars(list_t::reference x) // use a reference, otherwise you create a copy
{
cout << x.first << " " << x.second << endl;
}
int main()
{
pair<string,char> mypair;
listPair.push_back(make_pair("bib",'a'));
listPair.push_back(make_pair("bob",'b'));
for_each(listPair.begin(), listPair.end(), std::ref(printStars)); // C++11
for_each(listPair.begin(), listPair.end(), std::ptr_fun(&printStars)); // C++98
return 0;
}
Your problem is, your printStars() expects a list, however for_each passes it each item, not the actual list:
Working code :
#include <iostream>
#include <algorithm>
#include <iterator>
#include <list>
#include <string>
#include <utility>
list<pair<string,char> > listPair;
list<pair<string,char> >::iterator it;
void printStars(const pair<string,char> & listPair){ //notice the &, so it would pass by reference and not make a new copy of the pair.
cout << listPair.first << ' ';
}
int main() {
pair<string,char> mypair;
listPair.push_back(make_pair("bib",'a'));
listPair.push_back(make_pair("bob",'b'));
for_each(listPair.begin(), listPair.end(), printStars);
cout << endl;
return 0;
}