I am trying to generate boost::uuids::uuid from boost::compute::detail::sha1 in this way:
#include <iostream>
#include "boost/uuid/uuid.hpp"
#include "boost/uuid/uuid_io.hpp"
#include "boost/uuid/string_generator.hpp"
#include "boost/compute/detail/sha1.hpp"
int main(int argc, char* argv[])
{
try
{
boost::compute::detail::sha1 sha1("b888e35f9edf3794760392e1066d69-f43d-452e-8475-a09bae9a2e8500000000-0000-0000-0000-000000000000");
std::string str = sha1;
boost::uuids::uuid uuid = boost::uuids::string_generator()(str); // ERROR HERE!!
}
catch (std::exception& e)
{
std::cerr << "Error occurred: " << e.what() << std::endl;
}
return 0;
}
But this code fails with error Error occurred: invalid uuid string (See above)
I am using Visual Studio 2017, Boost 1.67
Where is my mistake? How to generate boost::uuids::uuid from boost::compute::detail::sha1
PS: That code worked on previous boost versions.
The proper, supported approach to getting a UUID from the SHA1 hash of an arbitrary string is as follows:
#include <string_view>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/name_generator_sha1.hpp>
boost::uuids::uuid uuid_from_string(std::string_view const input) {
static constexpr boost::uuids::uuid ns_id{}; // †null root, change as necessary
return boost::uuids::name_generator_sha1{ns_id}(input.data(), input.size());
}
Online Demo
(std::string_view is used for exposition; use std::string or char const* or whatever as appropriate if this isn't ideal for you.)
While this is the correct approach, there are two important notes:
As per RFC 4122, you need to provide a namespace for your UUID; basically this is salt for your SHA1 hash. There are predefined namespaces for DNS names, URLs, ISO OIDs, and X.500 distinguished names, but as your input doesn't appear to match any of those you need to define your own; as indicated on the line marked †, the null namespace is used here for exposition. A more detailed explanation of UUID namespaces can be found in this SO answer: Generating v5 UUID. What is name and namespace?
The output from this code will differ entirely from the output from the code in your question; iff you need the output to match the old code you can do the following:
#include <cstring>
#include <string_view>
#include <boost/endian/conversion.hpp>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/detail/sha1.hpp>
boost::uuids::uuid uuid_from_string_old(std::string_view const input) {
boost::uuids::detail::sha1::digest_type digest;
{
boost::uuids::detail::sha1 h;
h.process_bytes(input.data(), input.size());
h.get_digest(digest);
}
boost::uuids::uuid ret;
auto p = ret.begin();
for (std::size_t i{}; i != 4; p += 4, ++i) {
auto const d = boost::endian::native_to_big(digest[i]);
std::memcpy(p, &d, 4);
}
return ret;
}
Online Demo
This produces the same output, as can be seen from this demo using the old code with Boost 1.65.1. This goes against my commented policy of never directly using someone else's detail namespace, but unless you can find the magic namespace id (if one exists), this is the only approach using Boost that I'm aware of. N.b. this fixes a bug in the old Boost code for big-endian machines; if you need to retain that bug, change the call to boost::endian::native_to_big to invoke boost::endian::endian_reverse instead.
Related
#include <iostream>
#include <numeric>
#include <ranges>
#include <vector>
#include <string>
#include <string_view>
int main() {
auto str = (
std::views::iota(1)
| std::ranges::views::take(5)
| std::ranges::views::transform([](int x) -> std::string_view {
return std::to_string(x) + "|";
})
| std::ranges::views::join
);
for (const char ch : str) {
std::cout << ch;
}
return 0;
}
I'm new to functional programming in cpp. I want to generate first five natural numbers and translate them to string, then join them and print it.
If I use std::string for return type of lambda for transform, it throws many error on compile time. I thought that I should change it into std::string_view. I changed so, and it compiled without compile error. However, if I use std::string_view there, the lambda function returns only reference for string, and translated string that on stack memory is removed on memory when lambda ends. So, the program doesn't print anything.
How can I fix it?
If I use std::string for return type of lambda for transform, it
throws many error on compile time.
What you have observed is a C++20 defect that has been resolved by P2328. If you use a newer compiler version that has already implemented P2328 (such as gcc-11.2), your code will be well-formed.
Before P2328, I think there is no simple solution in the standard.
I wrote a code that sorts vector strings by length. Unfortunately, I'm not sure whether it will work in the next standard in this form. Is this the correct code in C++20?
#include <algorithm>
#include <iostream>
#include <string>
#include <ranges>
#include <vector>
int main() {
std::vector<std::string> words = {"std", "vector", "string", "optional", "clamp"};
// C++11
// std::sort(words.begin(), words.end(),
// [](auto& lhs, auto& rhs) { return lhs.length() < rhs.length(); });
// maybe C++20?
using namespace std::ranges;
sort(words, {}, size); // or 'sort(words, less{}, size);'
for (auto& word : words) {
std::cout << word << "\n";
}
}
Your code is fine and compiles under the upcoming GCC 10.
As #Ayxan points out, C++20 will still have the usual algorithms, so you don't need to change your code if you don't want.
When you have some code using only standard or very-commonly-used libraries, you can try compiling it with newer compilers and experimental standard versions, on sites like:
Compiler Explorer - https://godbolt.org/
Coliru ("COmpile, LInk and RU") - http://coliru.stacked-crooked.com/
and others.
So I recently discovered the use of map and vectors, however, I'm having trouble of trying to figure a way to loop through a vector containing strings.
Here's what I've tried:
#include <string>
#include <vector>
#include <stdio>
using namespace std;
void main() {
vector<string> data={"Hello World!","Goodbye World!"};
for (vector<string>::iterator t=data.begin(); t!=data.end(); ++t) {
cout<<*t<<endl;
}
}
and when I try to compile it, I get this error:
cd C:\Users\Jason\Desktop\EXB\Win32
wmake -f C:\Users\Jason\Desktop\EXB\Win32\exbint.mk -h -e
wpp386 ..\Source\exbint.cpp -i="C:\WATCOM/h;C:\WATCOM/h/nt" -w4 -e25 -zq -od -d2 -6r -bt=nt -fo=.obj -mf -xs -xr
..\Source\exbint.cpp(59): Error! E157: col(21) left expression must be integral
..\Source\exbint.cpp(59): Note! N717: col(21) left operand type is 'std::ostream watcall (lvalue)'
..\Source\exbint.cpp(59): Note! N718: col(21) right operand type is 'std::basic_string<char,std::char_traits<char>,std::allocator<char>> (lvalue)'
Error(E42): Last command making (C:\Users\Jason\Desktop\EXB\Win32\exbint.obj) returned a bad status
Error(E02): Make execution terminated
Execution complete
I tried the same method using map and it worked. The only difference was I changed the cout line to:
cout<<t->first<<" => "<<t->last<<endl;
Add iostream header file and change stdio to cstdio.
#include <iostream>
#include <string>
#include <vector>
#include <cstdio>
using namespace std;
int main()
{
vector<string> data={"Hello World!","Goodbye World!"};
for (vector<string>::iterator t=data.begin(); t!=data.end(); ++t)
{
cout<<*t<<endl;
}
return 0;
}
#include <iostream>
#include <vector>
#include <string>
int main()
{
std::vector<std::string> data = {"Hello World!", "Goodbye World!"};
for (std::vector<std::string>::iterator t = data.begin(); t != data.end(); t++) {
std::cout << *t << std::endl;
}
return 0;
}
Or with C++11 (or higher):
#include <iostream>
#include <vector>
#include <string>
typedef std::vector<std::string> STRVEC;
int main()
{
STRVEC data = {"Hello World!", "Goodbye World!"};
for (auto &s: data) {
std::cout << s << std::endl;
}
return 0;
}
From the Open Watcom V2 Fork-Wiki on the C++ Library Status page:
<string>
Mostly complete. Although there are no I/O operators, all other member functions and string operations are available.
A workaround (besides implementing the << operator) would be asking the string instances for the C string:
for (vector<string>::iterator t = data.begin(); t != data.end(); ++t) {
cout << t->c_str() << endl;
}
This of course only works as long as the strings don't contain zero byte values.
When I compile your code, I get:
40234801.cpp:3:17: fatal error: stdio: No such file or directory
#include <stdio>
^
You clearly have a header called "stdio" in your include path that you haven't shown us.
If you change that line to the standard #include <iostream>, then the only reported error is that you wrote void main() instead of int main(). Fix that, and it will build and run.
In passing, note also that using namespace should be avoided.
I found a solution to my own issue. Instead of using a c_str, I used std::string and switched to using the G++ compiler instead of Open Watcom
Instead of having:
char *someString="Blah blah blah";
I instead replaced it with:
string someString="Blah blah blah";
This way is much more efficient and easier.
I was attempting to follow the example of Finite State Filters in the Boost::iostreams documentation. However when I went to use the filter I got an error stating the ::imbue was not accessible because 'boost::iostreams::detail::finite_state_filter_impl' uses 'protected' to inherit from 'my_fsm'.
Frustrated I copied my code into the tests used to in the boost examples. The tests compile and pass. My conculsion is that I am probably mis-using the dual use filter defined by:
typedef io::finite_state_filter my_fsm_filter;
I feel that just pushing it onto a filtered_stream may not be proper, but I could not find a missing step. I am sure there must be a need to wrap the filter but I can find no example (though I am sure if I dug deep enough into the code used to test the boost code it has to be there somewhere).
here is a bit of example code:
#include <boost/mpl/vector.hpp>
#include <libs/iostreams/example/finite_state_filter.hpp>
namespace io = boost::iostreams;
struct my_fsm : io::finite_state_machine<my_fsm> {
BOOST_IOSTREAMS_FSM(my_fsm) // define skip and push.
typedef my_fsm self;
static const int beginline = 0;
static const int skipline = 1;
static const int dataline = 2;
typedef boost::mpl::vector <
row<beginline, is<'C'>, skipline, &self::skip>,
row<beginline, is_any, dataline, &self::push>,
row<skipline, is<'\n'>, beginline, &self::skip>,
row<skipline, is_any, skipline, &self::skip>,
row<dataline, is<'\n'>, beginline, &self::push>,
row<dataline, is_any, dataline, &self::push>
> transition_table;
};
typedef io::finite_state_filter<my_fsm> my_fsm_filter;
#include <iostream>
#include <string>
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/stream.hpp>
namespace io = boost::iostreams;
int main() {
io::stream<io::file_sink> out(io::file_sink("outputfile.txt"));
io::filtering_istream in;
my_fsm_filter infsm;
in.push(my_fsm_filter());
in.push(io::file_source("inputdata.txt"));
while (in) {
std::string line;
if(std::getline(in, line)) {
//std::cout << line << std::endl;
out << line << std::endl;
}
}
return 0;
}
I personally feel that there is a bug in the sample header with respect to this imbue call.
However, you can work around it by changing the typedef to
struct my_fsm_filter : io::finite_state_filter<my_fsm> {
using io::finite_state_filter<my_fsm>::imbue;
};
This explicitly exposes the imbue method as public on the derived type. I haven't looked at the sample program that you reported to be working (because you didn't link to it). But it's possible they used a similar hack.
In my tests, a similar edit to finite_state_filte.hpp L278 to add
using base_type::imbue;
to class finite_state_filter has the same effect.
I'm trying to find an element in a vector of structs. The code works when searching in a case-sensitive manner. When I try enhancing it to be case-insensitive, I run into two issues.
Simply including boost/algorithm/string.hpp breaks the previously working VS2010 build. The error is "'boost::phoenix::bind' : ambiguous call to overloaded function". Builds OK in Xcode. Any way to disambiguate the bind?
I guess I've got the syntax wrong in the second (commented out) find_if line, adding the istarts_with call. I get errors from the phoenix headers saying "error: no type named 'type'". Assuming issue #1 can be fixed, how should I correct this line?
Thanks!
Code:
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <boost/algorithm/string.hpp> // This include breaks VS2010!
#include <boost/phoenix/bind.hpp>
#include <boost/phoenix/core.hpp>
#include <boost/phoenix/operator.hpp>
#include <boost/phoenix/stl/algorithm.hpp>
using namespace boost::phoenix;
using boost::phoenix::arg_names::arg1;
using boost::istarts_with;
using std::string;
using std::cout;
// Some simple struct I'll build a vector out of
struct Person
{
string FirstName;
string LastName;
Person(string const& f, string const& l) : FirstName(f), LastName(l) {}
};
int main()
{
// Vector to search
std::vector<Person> people;
std::vector<Person>::iterator dude;
// Test data
people.push_back(Person("Fred", "Smith"));
// Works!
dude = std::find_if(people.begin(), people.end(), bind(&Person::FirstName, arg1) == "Fred");
// Won't build - how can I do this case-insensitively?
//dude = std::find_if(people.begin(), people.end(), istarts_with(bind(&Person::FirstName, arg1), "Fred"));
if (dude != people.end())
cout << dude->LastName;
else
cout << "Not found";
return 0;
}
You'd need two binds to make it work. First define:
int istw(string a, string b) { return istarts_with(a,b); }
and then use the following as the predicate for the find_if:
bind(&istw,bind(&Person::FirstName, arg1),"fred")
Two comments:
Make sure you're using the right bind, namely, use boost::phoenix::bind.
The definition of istw is probably unnecessary but I could not find the right way to replace it.