The following trivial usage of Boost's locale library generates a runtime error:
#include <boost/locale.hpp>
int main(int argc, const char * argv[]) {
boost::locale::generator gen;
std::locale loc = gen("");
std::locale::global(loc);
std::wstring a=L"Façade", b=L"facade";
bool eq = std::use_facet<boost::locale::collator<wchar_t>>(loc).compare(
boost::locale::collator_base::secondary,
a, b
) == 0;
if (eq) std::cout << "OK" << std::endl;
return 0;
}
The debugger traps the error in collator.hpp here:
int compare(level_type level,string_type const &l,string_type const &r) const
{
---> return do_compare(level,l.data(),l.data()+l.size(),r.data(),r.data()+r.size());
}
The error is "Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)"
I'm on a late 2013 iMac running macOS Sierra version 10.12 with Xcode 8.0 (8A218a) and boost-1.61.0_1. In hopes that maybe I just failed to install ICU with Boost, I just ran this:
brew install boost --with-icu4c --cxx11
This sample is based on Boost's own documentation:
http://www.boost.org/doc/libs/1_49_0/libs/locale/doc/html/index.html
Thoughts? Thank you.
---- EDIT -----
I tried dumping out info about the default locale:
std::cout << std::use_facet<boost::locale::info>(loc).name() << std::endl;
It contained simply "C" and the other attributes were not what I expected. I thus got explicit in my setup:
boost::locale::generator gen;
std::locale loc = gen("en_US.UTF-8");
std::locale::global(loc);
std::cout << std::use_facet<boost::locale::info>(loc).name() << std::endl;
std::cout << std::use_facet<boost::locale::info>(loc).language() << std::endl;
std::cout << std::use_facet<boost::locale::info>(loc).country() << std::endl;
std::cout << std::use_facet<boost::locale::info>(loc).variant() << std::endl;
std::cout << std::use_facet<boost::locale::info>(loc).encoding() << std::endl;
std::cout << std::use_facet<boost::locale::info>(loc).utf8() << std::endl;
Which output:
en_US.UTF-8
en
US
utf-8
1
Sadly, this did not help. Also, I noticed that the following code didn't work as expected:
const std::string g = "grüßEN";
std::cout << boost::locale::normalize(g, boost::locale::norm_nfc) << std::endl;
std::cout << boost::locale::normalize(g, boost::locale::norm_nfd) << std::endl;
std::cout << boost::locale::normalize(g, boost::locale::norm_nfkc) << std::endl;
std::cout << boost::locale::normalize(g, boost::locale::norm_nfkd) << std::endl;
It output all the same strings:
grüßEN
grüßEN
grüßEN
grüßEN
This seem to suggest there's something screwy with my install of Boost or ICU.
Related
I have the following C++ code to parse a C++ code in a string:
std::string cpp_code = " static int foo(int a, float b)\n{\n/* here could be your code */\n}\n";
std::string function_regex_str = R"(\s*(\w+)?\s+(\w+)\s+(\w+)\((.*)\)\s+\{\s+(.*)\s+\})";
std::regex function_regex(function_regex_str, std::regex::ECMAScript);
std::cmatch sm;
auto ret = std::regex_search(cpp_code.c_str(), sm, function_regex);
if (ret) {
std::cout << "fbound:\t" << sm[1] << std::endl;
std::cout << "ftype:\t" << sm[2] << std::endl;
std::cout << "fname:\t" << sm[3] << std::endl;
std::cout << "fparam:\t" << sm[4] << std::endl;
std::cout << "fbody:\t" << sm[5] << std::en)dl;
}
The code works fine. Now the first group (sm[1]) should be optional. So I appended ? to the first group (\w+). But if I tested the code with the shorten string
cpp_code = "int foo(int a, float b)\n{\n/* here could be your code */\n}\n"
regex_search returns false.
How can I make the first group (in the code above for the substring static) optional?
I tested the code with Visual Studio 2022 C++.
Using the following bit of code compiled against boost 1.62:
#include <boost/rational.hpp>
#include <iostream>
int main() {
auto val = boost::rational<int64_t>(499999, 2);
std::cout << val << std::endl;
std::cout << boost::rational_cast<double>(val) << std::endl;
}
I get the following output:
499999/2
250000
I would expect rational_cast to output 249999.5
Can anyone explain what I am doing wrong?
Modify the default formatting for floating-point input/output:
std::cout << std::fixed << boost::rational_cast<double>(v) << std::endl; add std::fixed to it.
I try to convert form matrix in Opencv to matrix in armadillo
I found this func in this link
but when I try to call it give me error :
error C2783: 'arma::Mat cvMat2armaMat(cv::Mat &)' : could not deduce template argument for 'T3'
IntelliSense: no instance of function template "cvMat2armaMat" matches the argument list
This is function :
template<class T3>
arma::Mat <T3> cvMat2armaMat(cv::Mat & cvMatIn)
{
return armaConv (cvMatIn.data, cvMatIn.rows, cvMatIn.cols,false);
}
This is function calling ( the error appear when I call func )
cv::Mat CurrentImage = imread(path, 0);
arma::mat singleImage = cvMat2armaMat (CurrentImage);
what should I do ?
You need to specify the type of arma::mat<XXX> that cvMat2armaMat() returns.
What is armaConv?
Maybe you meant this:
template<class T3>
arma::Mat <T3> cvMat2armaMat(cv::Mat & cvMatIn)
{
return arma::Mat <T3> (cvMatIn.data, cvMatIn.rows, cvMatIn.cols,false);
}
and the call is this:
auto singleImage = cvMat2armaMat<float>(CurrentImage);
or you'll need to switch on the cv::Mat type.
I can answer how to do this on VC10 and g++ but I still have a few issues to be resolved. I am new to writing templated functions. It is often necessary to convert between cv::Mat and arma::mat matrix objects since Armadillo has more complete Linear Algebra facilities. OpenCV stores it's matrices in row-major order while Armadillo stores it's matrices in column-major order which is one component of this problem. I have created the following templated functions that have been tested in VC10 and g++ 4.9 to work on both float and double type matrices:
template<typename T>
static void Cv_mat_to_arma_mat(const cv::Mat_<T>& cv_mat_in, arma::Mat<T>& arma_mat_out)
{
cv::Mat_<T> temp(cv_mat_in.t()); //todo any way to not create a temporary?
#if defined(WIN32)
//This compiles on VC10 but not g++ 4.9 (on Debian with OpenCV 2.4.9.1 and Arma 4.320.0)
arma_mat_out = arma::Mat<T>(temp.ptr<T>(), //<<"error: expected primary-expression before '(' token"
static_cast<arma::uword>(temp.cols),
static_cast<arma::uword>(temp.rows),
true,
true);
#elif defined(LINUX)
//This compiles on both but is not as nice
arma_mat_out = arma::Mat<T>(reinterpret_cast<T*>(temp.data),
static_cast<arma::uword>(temp.cols),
static_cast<arma::uword>(temp.rows),
true,
true);
#endif
};
//This one is fine on both
template<typename T>
static void Arma_mat_to_cv_mat(const arma::Mat<T>& arma_mat_in,cv::Mat_<T>& cv_mat_out)
{
cv::transpose(cv::Mat_<T>(static_cast<int>(arma_mat_in.n_cols),
static_cast<int>(arma_mat_in.n_rows),
const_cast<T*>(arma_mat_in.memptr())),
cv_mat_out);
};
This code can be run with:
std::cout << "Testing arma::mat<->cv::Mat <double>:" << std::endl;
{
arma::Mat<double> A(3,2);
A << 1 << 2 << arma::endr
<< 3 << 4 << arma::endr
<< 5 << 6 << arma::endr;
cv::Mat_<double> cv_A(3,2);
M_math::Arma_mat_to_cv_mat<double>(A, cv_A);
std::cout << "Arma_mat_to_cv_mat<double>" << std::endl;
std::cout << A << std::endl;
std::cout << cv_A << std::endl;
std::cout << "Cv_mat_to_arma_mat<double>" << std::endl;
M_math::Cv_mat_to_arma_mat<double>(cv_A, A);
std::cout << cv_A << std::endl;
std::cout << A << std::endl;
}
std::cout << "Now <float>" << std::endl;
{
arma::Mat<float> A(3,2);
A << 1 << 2 << arma::endr
<< 3 << 4 << arma::endr
<< 5 << 6 << arma::endr;
cv::Mat_<float> cv_A(3,2);
M_math::Arma_mat_to_cv_mat<float>(A, cv_A);
std::cout << "Arma_mat_to_cv_mat<float>" << std::endl;
std::cout << A << std::endl;
std::cout << cv_A << std::endl;
std::cout << "Cv_mat_to_arma_mat<float>" << std::endl;
M_math::Cv_mat_to_arma_mat<float>(cv_A, A);
std::cout << cv_A << std::endl;
std::cout << A << std::endl;
}
My further questions are:
Any idea what I am doing wrong? This seems like it is a case of VC10 being more permissive than g++ but what am I missing if I want to keep using the first (nicer) version with g++?
In Cv_mat_to_arma_mat(...) are there any suggestions to get around the row-major order vs row-order issue without creating a temporary? cv_mat_in.t() does not alter the internal memory pointer in cv_mat_in, it is just a cv::MatExpr.
Any criticism of the style, safety, or performance of these functions would be appreciated. Of course they will not work with cv::Mat's that have more than one channel.
I just want to iterate through the members of an unordered map.
There are many simple examples on the web, including on this site, and yet none of them will compile. Apparently some examples are from a previous non-standard STL version, some are just old, and some are so new that my gcc 4.7.2 can't handle them. Please do not suggest the new auto iterator from C++11. I will get there some day when all my libraries are validated for that. Until then, I just want the old one to work. (see below for what I have tried)
Here is my test code:
#include <iostream>
#include <boost/unordered_map.hpp>
#include <string>
int main(int argc,char *argv[]) {
boost::unordered::unordered_map<std::string,int> umap;
//can't get gcc to accept the value_type()...
//umap.insert(boost::unordered_map::value_type("alpha",1));
//umap.insert(boost::unordered_map::value_type("beta",2));
//umap.insert(boost::unordered_map::value_type("gamma",3));
umap["alpha"]=1; //this works
umap["beta"]=2;
umap["gamma"]=3;
//can't get gcc to compile the iterator
//for (boost::unordered_map::iterator it=umap.begin();it!=umap.end();++it)
// std::cout << it->first <<", " << it->second << std::endl;
//gcc does not like it this way either
//for (int x=0;x<umap.size();x++)
// std::cout << x << " : " << umap[x].first << " = " << umap[x].second << std::endl;
//will gcc take this? No it does not
//for (int x=0;x<umap.size();x++)
// std::cout << x << " : " << umap[x] << std::endl;
//this does not work
//boost::unordered::unordered_map::iterator<std::string,int> it;
//this does not work
//boost::unordered::unordered_map::iterator it;
//for (it=umap.begin();it!=umap.end();++it)
// std::cout << it->first <<", " << it->second << std::endl;
//this does not work
//BOOST_FOREACH(boost::unordered_map::value_type value, umap) {
// std::cout << value.second;
// }
//std::cout << std::endl;
//this does not work either
//BOOST_FOREACH(boost::unordered_map::value_type<std::string,int> value, umap) {
// std::cout << value.second;
// }
//std::cout << std::endl;
std::cout << "umap size: " << umap.size() << std::endl;
std::cout << "umap max size: " << umap.max_size() << std::endl;
std::cout << "find alpha: " << (umap.find("alpha")!=umap.end()) << std::endl;
std::cout << "count beta: " << umap.count("beta") << std::endl;
}
Most of the errors are a variation of this:
error: 'template<class K, class T, class H, class P, class A> class boost::unordered::unordered_map' used without template parameters
Here is my build command:
g++ -I..\boost umap.cpp
I should be embarrassed for getting stuck on such a beginner's question, but from the volume of similar questions I am finding, this is just hard enough to stop a lot of people in their tracks. I have written hash containers before (back when it was recommended NOT to use STL) and I am very tempted to just write my own... but the right thing to do is learn to use as many existing tools as possible... help!
I've looked at the following questions on stackoverflow where I haven't found an answer:
iterate through unordered_map using boost_foreach
I tried:
BOOST_FOREACH(boost::unordered_map::value_type& value, umap) {
but it gives the same error I show below.
Unordered_map iterator invalidation
This one is close, but not quite my issue:
Iterator invalidation in boost::unordered_map
This one uses auto
and I can't switch compilers at this time.
C++ some questions on boost::unordered_map & boost::hash
This one is mostly about the theory of maps:
how to use boost::unordered_map
This is a rather complicated example, but you will see in my code I am already trying to declare iterators... they just won't compile.
How to use BOOST_FOREACH with an Unordered_map?
This is a nice example, but
it just does not compile. I tried a version of this in my code.
IT WORKS !
Here is the working code:
#include <iostream>
#include <boost/unordered_map.hpp>
#include <string>
int main(int argc,char *argv[]) {
boost::unordered::unordered_map<std::string,int> umap;
umap["alpha"]=1;
umap["beta"]=2;
umap["gamma"]=3;
boost::unordered::unordered_map<std::string,int>::iterator it;
for (it=umap.begin();it!=umap.end();++it)
std::cout << it->first <<", " << it->second << std::endl;
std::cout << "umap size: " << umap.size() << std::endl;
std::cout << "umap max size: " << umap.max_size() << std::endl;
std::cout << "find alpha: " << (umap.find("alpha")!=umap.end()) << std::endl;
std::cout << "count beta: " << umap.count("beta") << std::endl;
}
It was a syntax error. I was putting the type in the wrong place when declaring the iterator.
Thanks everyone for your responses.
try changing boost::unordered::unordered_map::iterator it; it to boost::unordered::unordered_map<std::string,int>::iterator it;
NOTE:
It is also possible, and a good idea in more complex situations, to create a typedef of it, such as typedef boost::unordered::unordered_map<std::string,int>::iterator UMapStringIntIt;, or whatever you may call it.
The answer is in the question, but the simple solution is here for your convenience:
#include <iostream>
#include <boost/unordered_map.hpp>
#include <string>
int main(int argc,char *argv[])
{
boost::unordered::unordered_map<std::string,int> umap;
umap["alpha"]=1;
umap["beta"]=2;
umap["gamma"]=3;
for ( auto it= umap.begin(); it != umap.end(); ++it )
std::cout << it->first <<", " << it->second << std::endl;
}
Can somebody please explain me how to enumerate a BOOST_ENUM using BOOST_FOREACH ?
The example below show that I got it to work with std::for_each, but not with BOOST_FOREACH.
Sample code :
BOOST_ENUM_VALUES( MyEnum,
const char *,
(xMin)("xMin")
(xMax)("xMax")
(yMin)("yMin")
(yMax)("yMax")
);
void blah(const MyEnum & val) // working demo with std_foreach
{
std::cout << "index=" << val.index() << " val=" << val.value() << " str=" << val.str() << std::endl;
}
void foo()
{
//BOOST_FOREACH : does not compile...
BOOST_FOREACH(MyEnum myEnum, MyEnum() ) // I tried to construct a "dummy" enum in order to use its iterator with no luck...
{
std::cout << "index=" << myEnum.index() << " val=" << myEnum.value() << " str=" << myEnum.str() << std::endl;
}
//std::for_each : works...
std::for_each(MyEnum::begin(), MyEnum::end(), blah);
}
Many thanks in advance!
Edit: as mentioned in the answer, the code does work with the newest codebase of boost.
Your example code above compiles and runs just fine for me with gcc 4.5.1 and vc2010 (after adding the corresponding #include's, that is). I tried with enum_rev4.6 from the vault. What compilation errors do you see?