473,473 Members | 1,549 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Transform algorithm 1-n, n-1 or m-n

Hi!

Often, I have some situations when I have to convert a vector of 10
elements to a vector of 30 elements or vice-versa. (the ratio is 1-3)

I want to use the std::transform do to these tasks, but I end up using
std::for_each

Is a special/custom iterator the only way to do that with
std::transform?
Example:

vector<intsource, destiny;
....
now source has 30 elements
destiny.resize(10);

transform(source.begin(),source.end(),destiny.begi n(),ThreeToOneConverter());
** how to write the ThreeToOneConverter functor in order to process and
skip three elements at a time instead of one?
Another example:

vector<intsource, destiny;
....
now source has 10 elements
destiny.resize(30);

transform(source.begin(),source.end(),ThreeAtRowIt erator(destiny),OneToThreeConverter());
it will generate more abominations :(
Only for_each is the solution to this case?

Diego Martins
HP

Oct 18 '06 #1
2 1490
Diego Martins wrote:
Often, I have some situations when I have to convert a vector of 10
elements to a vector of 30 elements or vice-versa. (the ratio is 1-3)

I want to use the std::transform do to these tasks, but I end up using
std::for_each
Why do you want to use transform? Some good reason or is an exercise?
Only for_each is the solution to this case?
You can use for example remove_copy_if with a predicate that use his
internal state to tell what elements to copy.

--
Salu2
Oct 18 '06 #2
For N to 1 conversion I might write a custom transform algorithm taking
a vector of N input iterators. Some other class might be better than
vector. A functors operator() would have to take the object of input
iterators by const ref and return the value to assign from.

BTW sizing the destination is not always necessary if an insert_iterator
is used.

For 1 to 3 I'd use for_each on the source objects and a functor.

Fraser.

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Oct 19 '06 #3

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

Similar topics

20
by: Steffen Brinkmann | last post by:
Hi! I tried to modify the transform algorithm in a way that it doesn't take iterators, but a reference to a container class and a value, because Mostly I need to do an operation of a container...
5
by: Alex Vinokur | last post by:
Functor-parameters in the for_each() and transform() algorithms are passed by value. Might it make sense to have also algorithms for_each2() and transform2() which pass Functor-parameters by...
11
by: TheDD | last post by:
Hello, i don't manage to use the tolower() function: #include <algorithm> #include <iostream> #include <locale> #include <string> using std::cout;
0
by: Alex Vinokur | last post by:
One more improvement has been done in the "Computing very large Fibonacci numbers" algorithm. Its new version can be seen at http://groups.google.com/groups?selm=2mk62rFo1mv0U1%40uni-berlin.de ...
1
by: Brian McGuinness | last post by:
I have a question about using the STL transform algorithm in a function. What I want to do is define a group of array classes to represent APL-style arrays (arrays in which the number of...
9
by: Saki Arkoudopoulos | last post by:
Hello, I'm trying to convert a string with mixed upper- and lowercase characters to all lowercase characters. I found the following code on the Internet: transform(temp.begin(), temp.end(),...
9
by: Patrick Guio | last post by:
Dear all, I am trying to use the std::transform algorithm to to the following vector< vector<char> >::iterator ik = keys.begin(); // key list iterator vector< vector<char> >::iterator is = ik;...
4
by: Dean Card | last post by:
Okay, so here is the situation. I have need to do some on-the-fly image creation. I have everything working great except for the last part of it, applying a perspective type transform to the...
8
by: Alex Buell | last post by:
I've just written the below as an exercise (don't worry about the lack of checking), but I was wondering why I needed to write a struct with an operator() as a parameter to supply to the STL...
11
by: Gerald I. Evenden | last post by:
Working on a Kubuntu 64bit system "c++ (GCC) 4.0.3". The following simple program extracted from p.497 & 499 of N.M.Josurris' "The C++ Standard Library ... " (file t.cpp): 1 #include <string>...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...
0
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...
0
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 ...

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.