473,626 Members | 3,119 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::transform to append to a string

Cheers,

I've been struggling all day today with this, maybe you could give me some
useful pointers.
I would like to (elegantly) concatenate a string out of all node values in
a map. I have to do it without using boost (as it is forbidden to use it
in our project).

(Forgive me for giving a non-compiling code.)

---- begin ----
map<int,stringm ;
string s;

transform(m.beg in(),m.end(),?? ??, select2nd<int,s tring>());
---- end ----

What would you recommend placing instead of ???? ?
I've an ugly solution where I declare (something like):

vector<stringv;
transform(m.beg in(),m.end(), back_inserterer (v), select2nd<int,s tring>());
and then of course I could build the string out of the vector.

Why can't I do something like:
transform(m.beg in(),m.end(),ba ck_inserter(s.b egin()), select2nd<int,s tring>());

Regards
kev
Mar 13 '08 #1
2 2715
kvnil wrote:
Cheers,

I've been struggling all day today with this, maybe you could give me some
useful pointers.
I would like to (elegantly) concatenate a string out of all node values in
a map. I have to do it without using boost (as it is forbidden to use it
in our project).

(Forgive me for giving a non-compiling code.)

---- begin ----
map<int,stringm ;
string s;

transform(m.beg in(),m.end(),?? ??, select2nd<int,s tring>());
---- end ----
What's wrong with a for-loop?

for ( map<int,string> ::const_iterato r iter = m.begin();
iter != m.end(); ++iter ) {
s.append( iter->second );
}

What would you recommend placing instead of ???? ?
An append_iterator . You can easily write one. Look at some code for
back_inserter and modify it to use append() instead of push_back().

In fact, you could go all the way and write a generic command_iterato r, that
takes a function object upon construction and calls then translates

*out_iter = value;

into

command( value );
I've an ugly solution where I declare (something like):

vector<stringv;
transform(m.beg in(),m.end(), back_inserterer (v), select2nd<int,s tring>());
and then of course I could build the string out of the vector.

Why can't I do something like:
transform(m.beg in(),m.end(),ba ck_inserter(s.b egin()),
select2nd<int,s tring>());
Because the elements in s are characters not strings?
Best

Kai-Uwe Bux
Mar 13 '08 #2
>... <snip...
>(Forgive me for giving a non-compiling code.)

---- begin ----
map<int,string m;
string s;
transform(m.be gin(),m.end(),? ???, select2nd<int,s tring>()); ---- end
----
What's wrong with a for-loop?

for ( map<int,string> ::const_iterato r iter = m.begin();
iter != m.end(); ++iter ) {
s.append( iter->second );
}
>What would you recommend placing instead of ???? ?
An append_iterator . You can easily write one. Look at some code for
back_inserter and modify it to use append() instead of push_back().
thanks! I'll try and keep everybody posted :)
In fact, you could go all the way and write a generic
command_iterato r, that takes a function object upon construction and
calls then translates

*out_iter = value;

into

command( value );
Nice - but I do not see the merit (no offence in any way - you've tremendously
helped with your previous suggestion). Indeed transform would perform the
*outer=value call,
but won't I be able to achieve the same with std::foreach? It already calls
a functor for each object.
So I would need to implement an adaptable binary function (since the binary
function from <functionrequir est const on functor arguments), bind1st it's
first argument to my appendee (the target string), and then std::foreach

Maybe it would be even possible to somehow adapt string::append (with mem_fun),
the only problem is that during std::foreach on a map I would get back a
pair<int,string and not a string. Don't see yet how to overcome this. Maybe
with select2nd.

Cheers and thanks!
Mar 13 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
8162
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is effectively impossible to forward declare std::string. (Yes I am aware that some libraries have a string_fwd.h header, but this is not portable.) That said, is there any real reason why I can't derive an otherwise empty
11
3639
by: Christopher Benson-Manica | last post by:
Let's say I have a std::string, and I want to replace all the ',' characters with " or ", i.e. "A,B,C" -> "A or B or C". Is the following the best way to do it? int idx; while( (idx=str.find_first_of(',')) >= 0 ) { str.replace( idx, 1, "" ); str.insert( idx, " or " ); }
4
3655
by: Steven T. Hatton | last post by:
This code works for dividing each element of a boost::array<> by a value of its element type: template <typename T, size_t S> inline boost::array<T, S>& operator/=( boost::array<T, S>& lhs, const T& rhs ) { std::transform( lhs.begin() , lhs.end() , lhs.begin() , std::bind2nd( std::divides<T>(), rhs ) );
22
13238
by: Jason Heyes | last post by:
Does this function need to call eof after the while-loop to be correct? bool read_file(std::string name, std::string &s) { std::ifstream in(name.c_str()); if (!in.is_open()) return false; char c; std::string str;
1
1106
by: bor_kev | last post by:
Hi! I'd like to know how to transform (or convert) a System :: String to a std :: string under Visual Studio (C++) 2005 Beta. Sincerely, bor_kev *---------------------------------* Posted at: http://www.GroupSrv.com
2
6552
by: Alex | last post by:
I don't know why this works: class C { public: C(int x, int y) : a(x), b(y) {} int a,b; }; float avg(const C* c) {
8
9043
by: Dilip | last post by:
I was reading the C++ templates (Josuttis et al) book and came across an example that left me scratching my head.. there is a function template: template<typename T, int val> T addvalue(T const& x) { return x + val; }
4
3345
by: Ney André de Mello Zunino | last post by:
Hello. The following program: #include <list> #include <iterator> #include <algorithm> #include <cmath> #include <iostream>
6
4960
by: Vijai Kalyan | last post by:
Hello, I am trying to use std::transform to take in a collection of strings, transform them and insert the result into an output container. So, I wrote something like this: std::wstring doSomething (std::wstring const& str) { // do something to copy of str and return new value }
11
2893
by: Jacek Dziedzic | last post by:
Hi! I need a routine like: std::string nth_word(const std::string &s, unsigned int n) { // return n-th word from the string, n is 0-based // if 's' contains too few words, return "" // 'words' are any sequences of non-whitespace characters // leading, trailing and multiple whitespace characters // should be ignored.
0
8196
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8701
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8637
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5571
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4090
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4196
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2623
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1807
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.