What are the STL string limits? - c++

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;
}

Related

C++ bitset strange value [duplicate]

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;
}

How can I store a text file in a stack using fstream

#include <iostream>
#include <stack>
#include <fstream>
using namespace std;
int main()
{
stack <fstream> webpages;
fstream web1;
fstream web2;
fstream web3;
web1.open("web1.txt",fstream::out);
web2.open("web2.txt",fstream::out);
web3.open("web3.txt",fstream::out);
web1.close();
webpages.push(web1);
cout << webpages.size() << endl;
system("pause");
}
When I compile the program I get the following error:
std::basic_fstream>::basic_fstream(const
std::basic_fstream> &): attempting to
reference a deleted function
so I assume the way I am doing this is wrong. Is there a way I can store fstream files in a stack?
I'm assuming you want to store the text in the files on your stack and not the actual fstream objects? If so, it will be easier to store the content of the files as strings.
#include <iostream>
#include <stack>
#include <fstream>
#include <sstream>
int main()
{
std::stack <std::string> webpages;
for(const auto& file : {"web1.txt", "web2.txt", "web3.txt"}) {
std::fstream page(file);
std::stringstream ss;
ss << page.rdbuf();
webpages.emplace(ss.str());
}
std::cout << webpages.size() << "\n"; // will print "3"
std::cout << "-- pages read --\n";
for(;!webpages.empty(); webpages.pop()) {
std::cout << webpages.top() << "\n";
}
}
Edit: As pointed out, it really looks like you want to open some files for writing, so here's a version for that.
#include <stack>
#include <fstream>
int main()
{
std::stack<std::fstream> webpages;
for(const auto& file : {"web1.txt", "web2.txt", "web3.txt"}) {
webpages.emplace(file, std::fstream::out);
}
for(;!webpages.empty(); webpages.pop()) {
webpages.top() << "some text\n";
}
return 0;
}

Lexical cast Partial conversion - Is it possible?

lexical_cast throws an exception in the following case. Is there a way to use lexical_cast and convert the string to integer.
#include <iostream>
#include "boost/lexical_cast.hpp"
#include <string>
int main()
{
std::string src = "124is";
int iNumber = boost::lexical_cast<int>(src);
std::cout << "After conversion " << iNumber << std::endl;
}
I understand, I can use atoi instead of boost::lexical_cast.
If I'm understanding your requirements correctly it seems as though removing the non-numeric elements from the string first before the lexical_cast will solve your problem. The approach I outline here makes use of the isdigit function which will return true if the given char is a digit from 0 to 9.
#include <iostream>
#include "boost/lexical_cast.hpp"
#include <string>
#include <algorithm>
#include <cctype> //for isdigit
struct is_not_digit{
bool operator()(char a) { return !isdigit(a); }
};
int main()
{
std::string src = "124is";
src.erase(std::remove_if(src.begin(),src.end(),is_not_digit()),src.end());
int iNumber = boost::lexical_cast<int>(src);
std::cout << "After conversion " << iNumber << std::endl;
}
The boost/lexical_cast uses stringstream to convert from string to other types,so you must make sure the string can be converted completely! or, it will throw the bad_lexical_cast exception,This is an example:
#include <boost/lexical_cast.hpp>
#include <iostream>
#include <string>
#define ERROR_LEXICAL_CAST 1
int main()
{
using boost::lexical_cast;
int a = 0;
double b = 0.0;
std::string s = "";
int e = 0;
try
{
// ----- string --> int
a = lexical_cast<int>("123");//good
b = lexical_cast<double>("123.12");//good
// -----double to string good
s = lexical_cast<std::string>("123456.7");
// ----- bad
e = lexical_cast<int>("abc");
}
catch(boost::bad_lexical_cast& e)
{
// bad lexical cast: source type value could not be interpreted as target
std::cout << e.what() << std::endl;
return ERROR_LEXICAL_CAST;
}
std::cout << a << std::endl; // cout:123
std::cout << b << std::endl; //cout:123.12
std::cout << s << std::endl; //cout:123456.7
return 0;
}

boost::iostreams::counter doesn't seem to work

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;
}

Run through a vector of member functions in boost for_each

I try to learn boost lambda expressions, and this is a thing that doesn't work out.
How can I run in the for_each a selected member of Holder?
#include <iostream>
#include <string>
#include <boost/assign.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
using namespace std;
using namespace boost::assign;
using namespace boost::lambda;
class Holder
{
public:
void vRun1(std::string s){ cout << "vRun1 " << s << endl; }
void vRun2(std::string s){ cout << "vRun2 " << s << endl; }
void vRun3(std::string s){ cout << "vRun3 " << s << endl; }
};
// --------------------
std::map< std::string, mem_fun_ref_t<void, Holder> > replacer;
insert(replacer)
("buh", std::mem_fun_ref(&Holder::vRun1))
("mar", std::mem_fun_ref(&Holder::vRun2))
("heu", std::mem_fun_ref(&Holder::vRun3));
Holder hol;
How do I call here the functions I have registered in the map<>?
for_each(replacer.begin(), replacer.end(), /* bind(_1, hol, it.first) */ );
The result should be
vRun1 buh
vRun2 mar
vRun3 heu
It looks as though you are overcomplicating it slightly. I'd use boost::function<> like so:
See it live on http://liveworkspace.org/code/b2c5a38d8c3499eefb6330a839a89d0a
#define BOOST_RESULT_OF_USE_DECLTYPE
#include <iostream>
#include <string>
#include <map>
#include <boost/function.hpp>
#include <boost/foreach.hpp>
#include <boost/phoenix.hpp>
using namespace std;
class Holder {
public:
void vRun1(std::string s){ cout << "vRun1 " << s << endl; }
void vRun2(std::string s){ cout << "vRun2 " << s << endl; }
void vRun3(std::string s){ cout << "vRun3 " << s << endl; }
};
typedef std::map< std::string, boost::function<void(Holder&, std::string)> > Replacer;
int main()
{
Replacer replacer;
replacer["a"] = &Holder::vRun1;
replacer["b"] = &Holder::vRun2;
replacer["c"] = &Holder::vRun3;
Holder hol;
for (Replacer::const_iterator it=replacer.begin(); it != replacer.end(); ++it)
{
(it->second)(hol, it->first);
}
Alternatively:
std::cout << "Using BOOST_FOREACH:\n";
BOOST_FOREACH(Replacer::value_type& pair, replacer)
{
(pair.second)(hol, pair.first);
}
Or even:
std::cout << "Using Boost Phoenix:\n";
namespace phx = boost::phoenix;
using namespace phx::arg_names;
std::for_each(replacer.begin(), replacer.end(),
phx::bind(
phx::bind(&Replacer::value_type::second, arg1),
phx::ref(hol),
phx::bind(&Replacer::value_type::first, arg1)));
}
Outputs
vRun1 a
vRun2 b
vRun3 c
Using BOOST_FOREACH:
vRun1 a
vRun2 b
vRun3 c
Using Boost Phoenix:
vRun1 a
vRun2 b
vRun3 c
This works for me:
#include <iostream>
#include <string>
#include <boost/assign.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
#include <boost/function.hpp>
using namespace boost::lambda;
class Holder
{
public:
void vRun1(std::string s){ std::cout << "vRun1 " << s << std::endl; }
void vRun2(std::string s){ std::cout << "vRun2 " << s << std::endl; }
void vRun3(std::string s){ std::cout << "vRun3 " << s << std::endl; }
};
// --------------------
typedef std::map <std::string,
boost::function<void(Holder&, std::string)> > Replacer_t;
Replacer_t replacer;
typedef Replacer_t::value_type ReplacerValue_t;
int main()
{
boost::assign::insert(replacer)
(std::string("buh"), &Holder::vRun1)
(std::string("mar"), &Holder::vRun2)
(std::string("heu"), &Holder::vRun3);
Holder hol;
for_each(replacer.begin(),
replacer.end(),
bind(protect(bind(bind(&ReplacerValue_t::second,_2),
_1,
bind(&ReplacerValue_t::first,_2))),
boost::ref(hol), _1));
}
This took about 2 hours of fighting with boost::lambda. In C++11 I would write it in 30 seconds, so update your compilers accordingly.