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))();
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 want to output what one of my functions prints out into a separate textfile. I have tried what I have below but I get an error saying "invalid operands to binary expression" when I try outfile << print();. Any help would be appreciated.
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
void printing();
int main() {
printing();
printFile();
}
void printing() {
cout << "Cheers";
}
void printFile() {
ofstream cout("World.txt", ios::out);
if (cout) {
cout << print();
}
}
print() returns a void, so you can't use it in a ... << print() expression. Make print() take the desired output stream as a parameter instead, eg:
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include "Lab.h"
using namespace std;
void print(ostream &out = cout);
int main() {
print();
printToTextFile();
}
void print(ostream &out) {
out << "Cheers";
}
void printToTextFile() {
ofstream outfile("World.txt");
if (outfile) {
print(outfile);
}
}
I'm trying to make a program with #include <future> library.
When I try to access a header function, I get an error.
no instance of overloaded function "async" matches the argument list -- argument types are: (std::launch, int ()) [line 16, 14]
a pointer to a bound function may only be used to call the function
[line 16, 37]
main.cpp:
#include "TEST.h"
#include <future>
#include <iostream>
using namespace std;
using namespace Class;
FNH f;
int main(){
auto fn = async(launch::async, f.selam);
}
TEST.h:
#pragma once
#include <iostream>
using namespace std;
namespace Class{
class FNH{
public:
int selam(){
cout << "selam";
return 1;
}
};
}
I'm a beginner at coding so I really don't know how to fix it or if it's possible.
You can pass a member function pointer and the instance of the class it will be called on:
#include <future>
#include <iostream>
namespace Class {
class FNH {
public:
int selam(){
std::cout << "selam";
return 1;
}
int selam_2(int a, int b){
std::cout << "selam "<< a << " " << b;
return 1;
}
};
}
int main(){
Class::FNH f;
// Member Function Pointer
auto fn = std::async(std::launch::async, &Class::FNH::selam, f);
// Member Function Pointer with arguments
auto fn2 = std::async(std::launch::async, &Class::FNH::selam_2, f, 1, 2);
}
Put the method call in a lambda
#include <future>
#include <iostream>
namespace Class{
class FNH {
public:
int selam(){
std::cout << "selam";
return 1;
}
};
}
int main(){
Class::FNH f;
auto fn = std::async(std::launch::async, [&f]{ return f.selam(); });
}
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;
}
how can i pass a struct parameter by reference c++, please see below the code.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std;
struct TEST
{
char arr[20];
int var;
};
void foo(char * arr){
arr = "baby"; /* here need to set the test.char = "baby" */
}
int main () {
TEST test;
/* here need to pass specific struct parameters, not the entire struct */
foo(test.arr);
cout << test.arr <<endl;
}
The desired output should be baby.
I would use std::string instead of c arrays in c++
So the code would look like this;
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>
using namespace std;
struct TEST
{
std::string arr;
int var;
};
void foo(std::string& str){
str = "baby"; /* here need to set the test.char = "baby" */
}
int main () {
TEST test;
/* here need to pass specific struct parameters, not the entire struct */
foo(test.arr);
cout << test.arr <<endl;
}
That's not how you want to assign to arr.
It's a character buffer, so you should copy characters to it:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std;
struct TEST
{
char arr[20];
int var;
};
void foo(char * arr){
strncpy(arr, "Goodbye,", 8);
}
int main ()
{
TEST test;
strcpy(test.arr, "Hello, world");
cout << "before: " << test.arr << endl;
foo(test.arr);
cout << "after: " << test.arr << endl;
}
http://codepad.org/2Sswt55g
It looks like you are using C-strings. In C++, you should probably look into using std::string. In any case, this example is passed a char array. So in order to set baby, you will need to do it one character at a time (don't forget \0 at the end for C-strings) or look into strncpy().
So rather than arr = "baby" try strncpy(arr, "baby", strlen("baby"))
It won't work for you beause of the reasons above, but you can pass as reference by adding a & to the right of the type. Even if we correct him at least we should answer the question. And it wont work for you because arrays are implicitly converted into pointers, but they are r-value, and cannot be converted into reference.
void foo(char * & arr);