std::copy a int vector but withing subtraction

Jan 15, 2019 at 3:30pm
I start with a int vector `row` and would like copy it to a address `ptr`.

But now I want to use a sinple way to save the differ with the min_element into the `ptr`. Can I do the job in one line?

Hint: we would get the min using `std::min`


1
2
3
4
5
6
7
8
9
10
11
12
  std::vector<uint64_t> row {16, 32, 30, 40, 50, 60, 70, 80, 90, 16};

  std::copy(row.begin(), row.end(), ptr);  // do the job but no substraction

  // what I want but in one line 
  // firstly calculate the differ 
  std::vector<uint64_t> differ {0, 16, 14, 24, 34, 44, 54, 64, 74, 0};
  // then try to std::copy
    std::copy(differ.begin(), differ.end(), ptr);  // do the job but no substraction


Last edited on Jan 15, 2019 at 3:58pm
Jan 15, 2019 at 3:44pm
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <valarray>
using namespace std;

int main()
{
  valarray<uint64_t> row{ 16, 32, 30, 40, 50, 60, 70, 80, 90, 16 };
  valarray<uint64_t> differ = row - row.min();
  
  for ( auto e : row    ) cout << e << " ";   cout << '\n';
  for ( auto e : differ ) cout << e << " ";   cout << '\n';
}
Last edited on Jan 15, 2019 at 3:47pm
Jan 15, 2019 at 3:50pm
@lastchance
Thanks.
I do my apologize to the statement. I want to combine (the substation and the copy) in one line.
Is that possible?
Jan 15, 2019 at 3:52pm
valarray<uint64_t> differ = row - row.min();
Doesn't that do it? Call it ptr instead of differ if you want.

I suppose you could also use http://www.cplusplus.com/reference/algorithm/transform/
if you are dead set on vectors.
Last edited on Jan 15, 2019 at 3:55pm
Jan 15, 2019 at 3:58pm
1
2
3
4
5
  // what I want but in one line 
  // firstly calculate the differ 
  std::vector<uint64_t> differ {0, 16, 14, 24, 34, 44, 54, 64, 74, 0};
  // then try to std::copy
    std::copy(differ.begin(), differ.end(), ptr);  // do the job but no subtraction 

After the subtraction, I would like to copy the modifed number to `ptr` this address.
I suppose `std::copy` is 100% what we need. The thing is how to modify the vector inside the `std::copy` function call.
Last edited on Jan 15, 2019 at 4:00pm
Jan 15, 2019 at 4:03pm
I want to combine (the substation and the copy) in one line.

Are you saying that you don't know how to print the two values in one line?
hint: '\n' is what tells the cursor to go to the next line. 0_0

Anyways if this is a homework don't just simply copy lastchance's code you will definitely get caught.
Jan 15, 2019 at 4:06pm
EDIT:
I want to combine (the substation and the std::copy) in one line.

I think there some misunderstood in my problem statement.
I suppose `std::copy` is 100% what we need. The thing is how to modify the vector inside the `std::copy` function call.

I am not asking with '\n'.
Jan 15, 2019 at 4:06pm
I'm missing your point, @CakeByTheOcean.

To avoid this XY problem getting even more obscure could you explain the broader picture, please. i.e. give a complete compileable code, and don't try to paraphrase whatever instruction you have been given.

Also, has "ptr" been pre-declared, and if so, what type of variable/container is it?


By the way, a "substation" is a bunch of electrical transformers stepping the power supply voltage up or down. I think you mean "subtraction".
Last edited on Jan 15, 2019 at 4:08pm
Jan 15, 2019 at 4:14pm
Yes. My bad. I mean substation.
We assume the ptr ist `uint32_t*` with pointing well allocated memory.
Jan 15, 2019 at 4:21pm
You can't do what you're saying with copy. copy (InputIterator first, InputIterator last, OutputIterator result); copy has only one overload and all of its arguments are for inputs and output ONLY.

Does the question need you to write your own function (and hence "one line")? In that case you could have just copy-pasted lastchance's code what's the confusion?
Jan 15, 2019 at 4:30pm
Hmm Not so strict. The reason that I ask, is that I would like to write it more beautiful but have also doubt, if it is possible. I tried with some lambda expression but no success.
Jan 15, 2019 at 4:38pm
Ahh so you thought the third parameter for copy was a functor, makes sense.
Jan 15, 2019 at 5:15pm
Here you go, @CakeByTheOcean ... all in one line. I prefer my valarray version, though.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
   vector<uint64_t> row{ 16, 32, 30, 40, 50, 60, 70, 80, 90, 16 };
   uint32_t *ptr = new uint32_t[row.size()];
   for ( int i = 0, m = *min_element( row.begin(), row.end() ); i < row.size(); i++ ) ptr[i] = row[i] - m;

   for ( auto e : row    ) cout << e << " ";   cout << '\n';
   for ( int i = 0; i < row.size(); i++ ) cout << ptr[i] << " ";   cout << '\n';
   delete [] ptr;
}

Last edited on Jan 15, 2019 at 5:17pm
Jan 16, 2019 at 3:00am
But if you were specified to use std::min then you will need at least two for-loops, one for finding min and another for the actual copying.

Or you would need one really ugly for-loop:
for( int i = 0, min = *row.begin(), bool = 1; i < row.size(); i++) { if(bool) min = std::min(row[i], min) else ptr[i] = row[i]; if(i==row.size() && bool--) i = 0; }

Hey okay maybe the for-loop has a block of statements but it's still one line ;)
And in that way since compilers don't read whitespaces you can fit an entire program worth 5 million lines of code into one line, but what have you gained?
Topic archived. No new replies allowed.