How to convert std::string into basic_string

#include <boost/interprocess/managed_shared_memory.hpp> #include <boost/interprocess/allocators/allocator.hpp> #include <boost/interprocess/containers/map.hpp> How to convert std::string into boost string. I am doing some benchmarking by inserting the std::string value instead of char_string(ypedef of basic_string) but i am getting error message. #include <boost/interprocess/containers/vector.hpp> #include <boost/interprocess/containers/string.hpp> #include <iostream> using namespace boost::interprocess; //Typedefs of allocators and containers typedef managed_shared_memory::segment_manager segment_manager_t; typedef allocator<void, segment_manager_t> void_allocator; typedef allocator<int, segment_manager_t> int_allocator; typedef vector<int, int_allocator> int_vector; typedef allocator<int_vector, segment_manager_t> int_vector_allocator; typedef vector<int_vector, int_vector_allocator> int_vector_vector; typedef allocator<char, segment_manager_t> char_allocator; typedef basic_string<char, std::char_traits<char>, char_allocator> char_string; class complex_data { int id_; char_string char_string_; int_vector_vector int_vector_vector_; public: //Since void_allocator is convertible to any other allocator<T>, we can simplify //the initialization taking just one allocator for all inner containers. complex_data(int id, const char *name, const void_allocator &void_alloc) : id_(id), char_string_(name, void_alloc), int_vector_vector_(void_alloc) {} //Other members... }; //Definition of the map holding a string as key and complex_data as mapped type typedef std::pair<const char_string, int> map_value_type; typedef std::pair<char_string, int> movable_to_map_value_type; typedef allocator<map_value_type, segment_manager_t> map_value_type_allocator; typedef map< char_string, int , std::less<char_string>, map_value_type_allocator> complex_map_type; int main () { //Remove shared memory on construction and destruction struct shm_remove { shm_remove() { shared_memory_object::remove("MySharedMemory"); } ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); } } remover; //Create shared memory managed_shared_memory segment(create_only,"MySharedMemory", 65536); //An allocator convertible to any allocator<T, segment_manager_t> type void_allocator alloc_inst (segment.get_segment_manager()); //Construct the shared memory map and fill it complex_map_type *mymap = segment.construct<complex_map_type> //(object name), (first ctor parameter, second ctor parameter) ("MyMap")(std::less<char_string>(), alloc_inst); string s = "hello"; char y = (char_string)s; for(int i = 0; i < 100; ++i) { char_string key_object(alloc_inst); map_value_type value(key_object, i); mymap->insert(value); } return 0; } error: no matching function for call to ‘boost::interprocess_container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0u>, boost::interprocess::iset_index>
::basic_string(boost::interprocess_container::string&)’ /usr/include/boost/interprocess/containers/container/string.hpp:591: note: candidates are: boost::interprocess_container::basic_string<CharT, Traits, Alloc>::basic_string(typename A::size_type, CharT, const A&) [with CharT = char, Traits = std::char_traits<char>, A = boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0u>, boost::interprocess::iset_index> ] /usr/include/boost/interprocess/containers/container/string.hpp:584: note: boost::interprocess_container::basic_string<CharT, Traits, Alloc>::basic_string(const CharT*, const A&) [with CharT = char, Traits = std::char_traits<char>, A = boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0u>, boost::interprocess::iset_index> ] /usr/include/boost/interprocess/containers/container/string.hpp:577: note: boost::interprocess_container::basic_string<CharT, Traits, Alloc>::basic_string(const CharT*, typename A::size_type, const A&) [with CharT = char, Traits = std::char_traits<char>, A = boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0u>, boost::interprocess::iset_index> ] /usr/include/boost/interprocess/containers/container/string.hpp:564: note: boost::interprocess_container::basic_string<CharT, Traits, Alloc>::basic_string(const boost::interprocess_container::basic_string<CharT, Traits, Alloc>&, typename A::size_type, typename A::size_type, const A&) [with CharT = char, Traits = std::char_traits<char>, A = boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0u>, boost::interprocess::iset_index> ] /usr/include/boost/interprocess/containers/container/string.hpp:557: note: boost::interprocess_container::basic_string<CharT, Traits, Alloc>::basic_string(boost::interprocess::rv<boost::interprocess_container::basic_string<CharT, Traits, Alloc> >&) [with CharT = char, Traits = std::char_traits<char>, A = boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0u>, boost::interprocess::iset_index> ] /usr/include/boost/interprocess/containers/container/string.hpp:548: note: boost::interprocess_container::basic_string<CharT, Traits, Alloc>::basic_string(const boost::interprocess_container::basic_string<CharT, Traits, Alloc>&) [with CharT = char, Traits = std::char_traits<char>, A = boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0u>, boost::interprocess::iset_index> ] /usr/include/boost/interprocess/containers/container/string.hpp:539: note: boost::interprocess_container::basic_string<CharT, Traits, Alloc>::basic_string(const A&) [with CharT = char, Traits = std::char_traits<char>, A = boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0u>, boost::interprocess::iset_index> ] /usr/include/boost/interprocess/containers/container/string.hpp:532: note: boost::interprocess_container::basic_string<CharT, Traits, Alloc>::basic_string(boost::interprocess_container::basic_string<CharT, Traits, Alloc>::reserve_t, size_t, const A&) [with CharT = char, Traits = std::char_traits<char>, A = boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0u>, boost::interprocess::iset_index> ]
Thanks in advance. -- View this message in context: http://www.nabble.com/How-to-convert-std%3A%3Astring-into-basic_string-tp254... Sent from the Boost - Users mailing list archive at Nabble.com.

manish4gupta wrote:
How to convert std::string into boost string. I am doing some benchmarking by inserting the std::string value instead of char_string(ypedef of basic_string) but i am getting error message.
To convert between two different types of basic_string, you need to use the constructor that takes two iterators.

Is this that constructor... basic_string& assign(InputIter first, InputIter last) { //Dispatch depending on integer/iterator const bool aux_boolean = containers_detail::is_convertible<InputIter, std::size_t>::value; typedef containers_detail::bool_<aux_boolean> Result; return this->priv_assign_dispatch(first, last, Result()); } I am new to boost-interprocess. I don't know much about this. Any example converting the std:: string/c-string into boost basic_string will help me. I will be really thankful to you. Mathias Gaunard-2 wrote:
manish4gupta wrote:
How to convert std::string into boost string. I am doing some benchmarking by inserting the std::string value instead of char_string(ypedef of basic_string) but i am getting error message.
To convert between two different types of basic_string, you need to use the constructor that takes two iterators.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- View this message in context: http://www.nabble.com/How-to-convert-std%3A%3Astring-into-basic_string-tp254... Sent from the Boost - Users mailing list archive at Nabble.com.

I am new to boost-interprocess. I don't know much about this. Any example converting the std:: string/c-string into boost basic_string will help me. I will be really thankful to you.
basic_string is not boost, it's plain STL. from <string>: typedef basic_string<char> string; To assign two different basic_strings: std::string s = "foobar"; std::basic_string<wchar_t> ws(s.begin(), s.end()); Chris -- echo mailto: NOSPAM !#$.'<*>'|sed 's. ..'|tr "<*> !#:2" org@fr33z3

This is ok but i have to convert string into boost string. if i use the method as suggested. string s = "hello"; std::basic_string<wchar_t> ts(s.begin(), s.end()); char_string y = ts; I am getting the following error. run.cpp: In function ‘int main()’: run.cpp:63: error: conversion from ‘std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >’ to non-scalar type ‘boost::interprocess_container::basic_string<char, std::char_traits<char>, boost::interprocess:allocator<char, boost::interprocess::segment_manager<char, :boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0u>, boost::interprocess::iset_index>
’ requested
I am new to boost-interprocess. I don't know much about this. Any example converting the std:: string/c-string into boost basic_string will help me. I will be really thankful to you.
basic_string is not boost, it's plain STL. from <string>: typedef basic_string<char> string; To assign two different basic_strings: std::string s = "foobar"; std::basic_string<wchar_t> ws(s.begin(), s.end()); Chris -- echo mailto: NOSPAM !#$.'<*>'|sed 's. ..'|tr "<*> !#:2" org@fr33z3 _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users -- View this message in context: http://www.nabble.com/How-to-convert-std%3A%3Astring-into-basic_string-tp254... Sent from the Boost - Users mailing list archive at Nabble.com.

I have tried both but i am getting the following error message In constructor ‘boost::interprocess_container::basic_string<CharT, Traits, Alloc>::basic_string(const CharT*, const A&) [with CharT = char, Traits = std::char_traits<char>, A = boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0u>, boost::interprocess::iset_index>
]’: run.cpp:63: error: no matching function for call to ‘boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0u>, boost::interprocess::iset_index> ::allocator()’ /usr/include/boost/interprocess/allocators/allocator.hpp:129: note: candidates are: boost::interprocess::allocator<T, SegmentManager>::allocator(const boost::interprocess::allocator<T, SegmentManager>&) [with T = char, SegmentManager = boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0u>, boost::interprocess::iset_index>] /usr/include/boost/interprocess/allocators/allocator.hpp:124: note: boost::interprocess::allocator<T, SegmentManager>::allocator(SegmentManager*) [with T = char, SegmentManager = boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0u>, boost::interprocess::iset_index>]
Igor R wrote:
This is ok but i have to convert string into boost string.
You mean Boost.Interprocess string.
std::string s1("test");
char_string s2(s1.begin(), s1.end()); or: char_string s2(s1.c_str()); _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- View this message in context: http://www.nabble.com/How-to-convert-std%3A%3Astring-into-basic_string-tp254... Sent from the Boost - Users mailing list archive at Nabble.com.

Hi, can you please send a small but complete sample code of what you are trying (a bunch of includes and a main function, not more than a few lines)? Regards, Roland manish4gupta wrote:
I have tried both but i am getting the following error message
In constructor ‘boost::interprocess_container::basic_string<CharT, Traits, Alloc>::basic_string(const CharT*, const A&) [with CharT = char, Traits = std::char_traits<char>, A = boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0u>, boost::interprocess::iset_index>
]’: run.cpp:63: error: no matching function for call to ‘boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0u>, boost::interprocess::iset_index> ::allocator()’ /usr/include/boost/interprocess/allocators/allocator.hpp:129: note: candidates are: boost::interprocess::allocator<T, SegmentManager>::allocator(const boost::interprocess::allocator<T, SegmentManager>&) [with T = char, SegmentManager = boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0u>, boost::interprocess::iset_index>] /usr/include/boost/interprocess/allocators/allocator.hpp:124: note: boost::interprocess::allocator<T, SegmentManager>::allocator(SegmentManager*) [with T = char, SegmentManager = boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0u>, boost::interprocess::iset_index>]
Igor R wrote:
This is ok but i have to convert string into boost string. You mean Boost.Interprocess string.
std::string s1("test");
char_string s2(s1.begin(), s1.end()); or: char_string s2(s1.c_str()); _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

I am trying to store the index file (keyword , position) where keyword is string while position is long. But before doing that i am doing some benchmarking for the given code in which map is created as key, value. Where key is char_string (typedef of basic_string) but when i read the data using C++, it will be string. So my intention is to convert string into char_string. For that i am testing to convert std:: string s = "test" into char_string. But getting the error message. Please do help me. #include <boost/interprocess/managed_shared_memory.hpp> #include <boost/interprocess/allocators/allocator.hpp> #include <boost/interprocess/containers/map.hpp> #include <boost/interprocess/containers/vector.hpp> #include <boost/interprocess/containers/string.hpp> #include <iostream> using namespace boost::interprocess; //Typedefs of allocators and containers typedef managed_shared_memory::segment_manager segment_manager_t; typedef allocator<void, segment_manager_t> void_allocator; typedef allocator<int, segment_manager_t> int_allocator; typedef vector<int, int_allocator> int_vector; typedef allocator<int_vector, segment_manager_t> int_vector_allocator; typedef vector<int_vector, int_vector_allocator> int_vector_vector; typedef allocator<char, segment_manager_t> char_allocator; typedef basic_string<char, std::char_traits<char>, char_allocator> char_string; class complex_data { int id_; char_string char_string_; int_vector_vector int_vector_vector_; public: //Since void_allocator is convertible to any other allocator<T>, we can simplify //the initialization taking just one allocator for all inner containers. complex_data(int id, const char *name, const void_allocator &void_alloc) : id_(id), char_string_(name, void_alloc), int_vector_vector_(void_alloc) {} //Other members... }; //Definition of the map holding a string as key and complex_data as mapped type typedef std::pair<const char_string, int> map_value_type; typedef std::pair<char_string, int> movable_to_map_value_type; typedef allocator<map_value_type, segment_manager_t> map_value_type_allocator; typedef map< char_string, int , std::less<char_string>, map_value_type_allocator> complex_map_type; int main () { //Remove shared memory on construction and destruction struct shm_remove { shm_remove() { shared_memory_object::remove("MySharedMemory"); } ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); } } remover; //Create shared memory managed_shared_memory segment(create_only,"MySharedMemory", 65536); //An allocator convertible to any allocator<T, segment_manager_t> type void_allocator alloc_inst (segment.get_segment_manager()); //Construct the shared memory map and fill it complex_map_type *mymap = segment.construct<complex_map_type> //(object name), (first ctor parameter, second ctor parameter) ("MyMap")(std::less<char_string>(), alloc_inst); std::string s1("test"); char_string s2(s1.c_str()); for(int i = 0; i < 100; ++i) { char_string key_object(alloc_inst); map_value_type value(key_object, i); mymap->insert(value); } return 0; } Roland Bock-2 wrote:
Hi,
can you please send a small but complete sample code of what you are trying (a bunch of includes and a main function, not more than a few lines)?
Regards,
Roland
manish4gupta wrote:
I have tried both but i am getting the following error message
In constructor ‘boost::interprocess_container::basic_string<CharT, Traits, Alloc>::basic_string(const CharT*, const A&) [with CharT = char, Traits = std::char_traits<char>, A = boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0u>, boost::interprocess::iset_index>
]’: run.cpp:63: error: no matching function for call to ‘boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0u>, boost::interprocess::iset_index> ::allocator()’ /usr/include/boost/interprocess/allocators/allocator.hpp:129: note: candidates are: boost::interprocess::allocator<T, SegmentManager>::allocator(const boost::interprocess::allocator<T, SegmentManager>&) [with T = char, SegmentManager = boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0u>, boost::interprocess::iset_index>] /usr/include/boost/interprocess/allocators/allocator.hpp:124: note: boost::interprocess::allocator<T, SegmentManager>::allocator(SegmentManager*) [with T = char, SegmentManager = boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0u>, boost::interprocess::iset_index>]
Igor R wrote:
This is ok but i have to convert string into boost string. You mean Boost.Interprocess string.
std::string s1("test");
char_string s2(s1.begin(), s1.end()); or: char_string s2(s1.c_str()); _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- View this message in context: http://www.nabble.com/How-to-convert-std%3A%3Astring-into-basic_string-tp254... Sent from the Boost - Users mailing list archive at Nabble.com.

manish4gupta wrote:
I am trying to store the index file (keyword , position) where keyword is string while position is long. But before doing that i am doing some benchmarking for the given code in which map is created as key, value. Where key is char_string (typedef of basic_string) but when i read the data using C++, it will be string. So my intention is to convert string into char_string. For that i am testing to convert std:: string s = "test" into char_string. But getting the error message. Please do help me.
Hi, sorry, but that is not even close to a minimal example. Your problem is somewhere here (as you stated in another mail): std::string s = "test"; // OK char_string cs(s.c_str(), alloc_inst); // OK and thanks string y = (char_string) cs; // pblm Is you could send a sample program with just these lines in the main function and the required includes/typedefs. Nothing more. Lets focus on the problem, please. Regards, Roland

Thanks it works..... But after loading into the map when i try to get the value how can i do the reverse.i.e converting char_string into string std::string s = "test"; // OK char_string cs(s.c_str(), alloc_inst); // OK and thanks string y = (char_string) cs; // pblm error message run.cpp:63: error: conversion from ‘char_string’ to non-scalar type ‘boost::interprocess_container::basic_string<char, std::char_traits<char>, std::allocator<char> >’ requested Christoph Gysin-3 wrote:
This is ok but i have to convert string into boost string. if i use the method as suggested.
string s = "hello"; char_string y = ts;
You probably want this:
char_string cs(s.c_str(), alloc_inst);
Chris -- echo mailto: NOSPAM !#$.'<*>'|sed 's. ..'|tr "<*> !#:2" org@fr33z3 _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- View this message in context: http://www.nabble.com/How-to-convert-std%3A%3Astring-into-basic_string-tp254... Sent from the Boost - Users mailing list archive at Nabble.com.

But after loading into the map when i try to get the value how can i do the reverse.i.e converting char_string into string
std::string s = "test"; // OK char_string cs(s.c_str(), alloc_inst); // OK and thanks string y = (char_string) cs; // pblm
string y(cs.c_str()); Chris -- echo mailto: NOSPAM !#$.'<*>'|sed 's. ..'|tr "<*> !#:2" org@fr33z3

Thank you very much. Now it works as suggested. Regards manish4gupta wrote:
Thanks it works..... But after loading into the map when i try to get the value how can i do the reverse.i.e converting char_string into string
std::string s = "test"; // OK char_string cs(s.c_str(), alloc_inst); // OK and thanks string y = (char_string) cs; // pblm
error message run.cpp:63: error: conversion from ‘char_string’ to non-scalar type ‘boost::interprocess_container::basic_string<char, std::char_traits<char>, std::allocator<char> >’ requested
Christoph Gysin-3 wrote:
This is ok but i have to convert string into boost string. if i use the method as suggested.
string s = "hello"; char_string y = ts;
You probably want this:
char_string cs(s.c_str(), alloc_inst);
Chris -- echo mailto: NOSPAM !#$.'<*>'|sed 's. ..'|tr "<*> !#:2" org@fr33z3 _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- View this message in context: http://www.nabble.com/How-to-convert-std%3A%3Astring-into-basic_string-tp254... Sent from the Boost - Users mailing list archive at Nabble.com.
participants (5)
-
Christoph Gysin
-
Igor R
-
manish4gupta
-
Mathias Gaunard
-
Roland Bock