This short C++ program behaves in a way which baffles me:
#include <cassert>
#include <iostream>
#include <string>
#include <boost/lexical_cast.hpp>
int main(void) {
signed char c = -2;
assert(c == -2);
c = boost::lexical_cast<signed char>(std::string("-2"));
std::cout << c << "\n";
}
Using g++ 5.2.1 and boost-1.58.0, I get:
terminate called after throwing an instance of 'boost::exception_detail::clone_impl >'
what(): bad lexical cast: source type value could not be interpreted as target
Why can't Boost cast from string "-2" to signed char given that the value -2 is representable by this type?
The solution is to use Boost:
#include <boost/lexical_cast.hpp>
#include <boost/numeric/conversion/cast.hpp>
int tmp = boost::lexical_cast<int>(std::string("-2"));
char c = boost::numeric_cast<signed char>(tmp);
Related
I want to set the bitset of the char '0' equal to 0101010101 but when I try I get the error "expected an identifier"
#include <iostream>
#include <string>
#include <bitset>
using namespace std;
int main() {
bitset<8> '0'=0101010101;
}
I've also tried
bitset <8> 0(string("0101010101"));
but I get the same error
You can use an unordered_map to set one to one mapping between an int and a bitset. The length of the sample 0101010101 is 10 so size of bitset will be 10 and 0101010101 = 341 in decimal.
#include <iostream>
#include <unordered_map>
#include <bitset>
std::unordered_map<int, std::bitset<10>> M {
{0, 341},
{1, ...},
...
...
...
};
int main()
{
std::cout << M[0] << std::endl;
}
0 is an int literal and '0' is a char literal, neither of which are variable names.
You can use _0 as variable name. Or even better use a name that describes what the variable is used for.
Warning: statement has no effect
Line 15
I have to display all the characters from s1 who are found in s2, too.
#include <iostream>
#include <string.h>
#include <ctype.h>
using namespace std;
int main()
{
char s1[250], s2[250];
unsigned int i;
cin.get(s1,250);
cin.get();
cin.get(s2,250);
for(i=0;i<strlen(s2);i++)
tolower(s2[i]);
for(i=0;i<strlen(s1);i++)
if(strchr(s2,tolower(s1[i])))
cout<<s1[i];
return 0;
}
std::tolower takes it's argument by value and returns the result, and therefore doesn't modify the input value.
I'm using C++ in Microsoft Visual Studio 12. I want to pass command line arguments. I have tried listing them in the MSVS's Project/Properties/Debugging/Command Arguments field and I've also tried using the CLIArgsMadeEasy add on but it never works. argc is always 1 where, of course, argv[0] is the app path.
Example: given a program of fred.exe that I would like to launch with three args : a,b,c
i.e. the equivalent of a cmd window line of
fred.exe a b c
I specify the args in the provided edit boxes exactly as:
a b c
using either method described above (MSVS standard or CLIArgsMadeEasy) but when I run they aren't passed.
The code is:
#include <iostream> // for standard I/O
#include <string> // for strings
#include <iomanip> // for controlling float print precision
#include <sstream> // string to number conversion
#include <math.h>
using namespace std;
int main(int argc, char *argv[])
{
...
I have tried this program in my visual studio and it works:
#include <iostream> // for standard I/O
#include <string> // for strings
#include <iomanip> // for controlling float print precision
#include <sstream> // string to number conversion
#include <math.h>
using namespace std;
int main(int argc, char *argv[])
{
for(int i = 1; i < argc; i++)
{
cout << i << ":" << argv[i] << endl;
}
return 0;
}
I am working with C++ in eclipse CDT and I am trying to convert string to uint64_t by using strtoull but everytime I get below error message -
..\src\HelloTest.cpp:39:42: error: strtoull was not declared in this scope
Below is my C++ example
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main() {
string str = "1234567";
uint64_t hashing = strtoull(str, 0, 0);
cout << hashing << endl;
}
return 0;
}
Is there anything wrong I am doing?
Why your solution doesn't work has already been pointed out by others. But there hasn't been a good alternative suggested yet.
Try this for C++03 strtoull usage instead:
#include <string>
#include <cstdlib>
int main()
{
std::string str = "1234";
// Using NULL for second parameter makes the call easier,
// but reduces your chances to recover from error. Check
// the docs for details.
unsigned long long ul = std::strtoull( str.c_str(), NULL, 0 );
}
Or, since C++11, do it directly from std::string via stoull (which is just a wrapper for the above, but saves on one include and one function call in your code):
#include <string>
int main()
{
std::string str = "1234";
// See comment above.
unsigned long long ul = std::stoull( str, nullptr, 0 );
}
Never use char[] or pointers if you have a working alternative. The dark side of C++, they are. Quicker, easier, more seductive. If once you start down the dark path, forever will it dominate your destiny, consume you it will. ;-)
the structure for strtoull is: strtoull(const char *, char * *, int)
You have given it a std::string as pointed out by #juanchopanza
This is the solution I came up with is
#include <iostream>
#include <cstring>
#include <string>
#include <cstdlib>
using namespace std;
int main() {
char str[] = "1234567";
unsigned long long ul;
char* new_pos;
charDoublePointer = 0;
ul = strtoull(str, &new_pos, 0);
cout << ul << endl;
return 0;
}
The output I got was: 1234567
Straight from the eclipse console.
Also at the end of your program you have return 0 out of scope with an extra curly brace.
Consider the following example:
#include <iostream>
#include <clocale>
#include <cstdlib>
#include <string>
int main()
{
std::setlocale(LC_ALL, "en_US.utf8");
std::string s = "03A0";
wchar_t wstr = std::strtoul(s.c_str(), nullptr, 16);
std::wcout << wstr;
}
This outputs Π on Coliru.
Question
std::strtoul, is from <cstdlib>. I'm perfectly fine with using it, but I was wondering if the above example was possible using only the C++ standard library (perhaps stringstreams)?
Note also that there is no prefex 0x on the string indicating hexadecimal.
Sure, std::stoul:
wchar_t wstr = std::stoul(s, nullptr, 16);
The main difference is the fact that it can throw exceptions for errors.