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;
}
Related
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
For example, I would need {20,6,143} to become {"20","6","143"}.
Simply using a range-based for-loop you could iterate through the vector of integers and using std::to_string you could convert each element to string and store to the new vector of strings.
#include <iostream>
#include <vector>
#include <string>
int main()
{
std::vector<int> vec{ 20,6,143 };
std::vector<std::string> strVec;
strVec.reserve(vec.size()); // reserve memory for unwanted reallocations
for (int element : vec)
strVec.emplace_back(std::to_string(element));
for (const auto& str : strVec)
std::cout << str << " ";
}
(See live online)
Alternatively using std::transform, you could transform the vector of integers as follows.
#include <iostream>
#include <vector>
#include <string>
#include <algorithm> // std::transform
#include <iterator> // std::cbegin
int main()
{
std::vector<int> vec{ 20,6,143 };
std::vector<std::string> strVec;
strVec.reserve(vec.size()); // reserve memory for unwanted reallocations
std::transform(std::cbegin(vec), std::cend(vec),
std::back_inserter(strVec), // insert to the back of strVec
[](const int element) { return std::to_string(element);}
);
for (const auto& str : strVec)
std::cout << str << " ";
}
(See live online)
std::transform combined with std::to_string should do the trick.
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;
}
Why is the output of this 0?
http://ideone.com/S7hgv
#include <boost/bind.hpp>
#include <vector>
#include <iostream>
using namespace std;
void f2(vector<int> &h)
{
h.clear();
h.push_back(0);
}
void f1(vector<int> &h)
{
boost::bind(f2, boost::ref(h));
}
int main()
{
vector<int> h;
f1(h);
cout << h.size() << "\n";
}
I need it to be 1, and for some reason h is not modified.
boost/std::bind() only constructs the function object. You still have to call it, in order for any code inside to execute.
To get the output of 1, replace the line
boost::bind(f2, boost::ref(h));
with
boost::bind(f2, boost::ref(h))();