This question already has answers here:
Using scanf/printf to input into/output from a bitset
(3 answers)
Closed 5 months ago.
#include <bitset>
#include <assert.h>
#include <stdio.h>
using namespace std;
int main()
{
bitset<128> bs(42);
bs[11]=0;
bs[12]=1;
assert(bs[12]==1);
printf("bs[11]=%d\n", bs[11]);
printf("bs[12]=%d\n", bs[12]);
return 0;
}
console output:
Why can't I simply get 0 or 1 as output ?
printf with %d is for integer values, whereas std::bitset::operator[] returns a std::bitset::reference.
You can use std::cout from <iostream> header (which is anyway a more c++ "way" to print to the console):
#include <bitset>
#include <assert.h>
#include <iostream>
int main()
{
std::bitset<128> bs(42);
bs[11] = 0;
bs[12] = 1;
assert(bs[12] == 1);
std::cout << "bs[11]=" << bs[11] << std::endl;
std::cout << "bs[12]=" << bs[12] << std::endl;
return 0;
}
Output:
bs[11]=0
bs[12]=1
A side note: better to avoid using namespace std - see here Why is "using namespace std;" considered bad practice?.
With some review comments :
#include <cassert>
#include <bitset>
#include <iostream>
// anything with a .h extension is probably "C" not "C++"
// #include <assert.h>
//#include <stdio.h>
// using namespace std; <== NO, don't use using namespace std;
int main()
{
std::bitset<128> bs(42);
bs[11]=0;
bs[12]=1;
assert(bs[12]==1);
std::cout <<"bs[11]" << bs[11] << "\n";
std::cout << "bs[12]" << bs[11] << "\n";
return 0;
}
If you are using C++ then don't call printf to output something (my compiler refuse to compile your code correctly).
This C++ code works correctly using iostream:
#include <bitset>
#include <iostream>
int main()
{
std::bitset<128> bs(42);
bs[11]=0;
bs[12]=1;
std::cout << "bs[11]=" << bs[11] << std::endl;
std::cout << "bs[12]=" << bs[12] << std::endl;
return 0;
}
Related
i'm having problem with this code :
#include <iostream>
#include <math.h>
#include <unistd.h>
#include <string>
#include <iostream>
#include <stdio.h>
using std::cout; using std::cerr;
using std::cin; using std::string;
using std::endl;
int main(int argc,char* argv[])
{
for(int x = 0; x <= 2013; x++)
{
cout << "Iteration: "<< x << "\r";
cout << "";
}
return 0;
}
i need my code to print "Iteration: 0" and than just refresh that 0 to 1,2,3,4.... everything on one console line. I used the carriage return but it doesn't work,the line are printed one after another as when i use "\n". The enviroment is linux 64 bit. The IDE is eclipse 8.01.
You have to put it at the beginning of the line:
cout << "\rIteration: "<< x;
EDIT: I have tested this modification of the original OP's code and it prints what he wants. Also, Oh dear look what I've found.
Also, as suggested by #Wintermute, you can do the following inside the for loop, for better visualization:
cout << "\rIteration: "<< x << std::flush;
sleep(1);
I am trying to cout some variables but the compiler says that cout is undefined. I have included iostream and am using namespace std. Removing using namespace std and using std::cout instead changes the issue to "namespace "std" has no member "cout" ". I found some answers saying to add # include "stdafx.h" to the code but Error: cannot open source file "stdafx.h" occurs.
Code is:
#include "Complex.h"
#include <cmath>
#include <iostream>
using namespace std;
Complex::Complex(int PolarOrRectang, float RealOrArg, float ImagOrAng) {
if (PolarOrRectang == 0) {
real = RealOrArg;
imag = ImagOrAng;
else {
real = RealOrArg * cos(ImagOrAng);
imag = RealOrArg * sin(ImagOrAng);
}
};
void Complex::getValue(int PolarOrRectang) {
if (PolarOrRectang == 0) {
cout << real << " +_" << imag << "i" << endl;
} else {
cout << sqrt((real^2) + (imag^2)) << "*e^-" << atan(imag / real)<< endl;
}
};
I'm trying to define a class, so my main is elsewhere.
Running a very basic program that just couts "hello world" works fine, the problem is specific to this code.
Put #include<iostream> at the first position, the order is important
#include "Complex.h"
#include <iostream>
#include <cmath>
PS: Why do you use std:: when you are using "using namespace std;"?
I wrote a code to test some iterator_category of C++ container's iterator's type.
#include <iostream> // std::cout
#include <iterator> // std::iterator_traits
#include <typeinfo> // typeid
#include <list>
#include <deque>
#include <stdio.h>
#include <vector>
#include <string>
#include <string.h>
using namespace std;
int main(int argc, char *argv[])
{
list<int>::iterator::iterator_category itr;
std::cout << typeid(itr).name() << endl;
std::cout << typeid(vector<int>::iterator::iterator_category).name() << endl;
std::cout << typeid(deque<int>::iterator::iterator_category).name() << endl;
std::cout << typeid(itr).name() << endl;
return 0;
}
I run this code in editplus.
But the result is weird.
What does the "St26" before the type mean?
I'm playing around with boost::iostreams and the user guide talks about filter "counter". So I try it with this code:
#include <iostream>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/device/null.hpp>
#include <boost/iostreams/filter/counter.hpp>
namespace io = boost::iostreams;
int main()
{
io::counter cnt;
io::filtering_ostream out(cnt | io::null_sink());
out << "hello";
std::cout << cnt.lines() << " " << cnt.characters() << std::endl;
}
It always gives
0 0
which doesn't seem to be what I am expecting.
A preliminary tracing with gdb suggests the counter object that is doing the counting has a different address with object 'cnt'. I suppose it is some kind of copying in pipeline? If that's the case, how can filter "counter" be of any use?
Looking at the documentation you can use either:
#include <iostream>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/device/null.hpp>
#include <boost/iostreams/filter/counter.hpp>
namespace io = boost::iostreams;
int main()
{
io::counter cnt;
io::filtering_ostream out(cnt | io::null_sink());
out << "hello";
std::cout << out.component<io::counter>(0)->lines() << " " << out.component<io::counter>(0)->characters() << std::endl;
}
or:
#include <iostream>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/device/null.hpp>
#include <boost/iostreams/filter/counter.hpp>
#include <boost/ref.hpp>
namespace io = boost::iostreams;
int main()
{
io::counter cnt;
io::filtering_ostream out;
out.push(boost::ref(cnt));
out.push(io::null_sink());
out << "hello";
std::cout << cnt.lines() << " " << cnt.characters() << std::endl;
}
What are the string limits for the Standard Template Library in C++?
#include <iostream>
#include <string>
int main() {
std::cout << std::string().max_size() << std::endl;
return 0;
}