|
You can use a while loop and use an iterator to achieve this.
|
|
|
|
|
|
|
vector <double> vt;
double x;
auto it = vt.begin(); while (it != vt.end())
{
*it /= x;
it++;
}
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
|
Hi,
I'm getting the following error:
error C2677: binary '/=' : no global operator found which takes type 'std::vector<_Ty>' (or there is no acceptable conversion)
|
|
|
|
|
The following compiles (and runs) fine (I've a rather old VS2005 compiler...)
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector <double> vt;
vt.push_back(7.0);
vt.push_back(5.0);
vt.push_back(3.0);
double x = 5.0;
vector<double>::iterator it = vt.begin();
while (it != vt.end())
{
*it /= x;
cout << *it << endl;
it++;
}
}
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Thanks for checking, could just be some weird option in my compiler.
|
|
|
|
|
With "Mystical-Powers".
There's a reason there are iterators in STL containers: you have to iterate.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Look at std::transform, it is your friend when you want to operate on all elements of a collection. And once you've worked out how to do that you can implement your own scalar division operator for collections:
template <typename collection>
collection operator/( const collection &c, typename collection::value_type divisor )
{
collection transformed;
std::transform( c.begin(), c.end(), std::back_inserter( transformed ), [divisor]( collection::value_type to_divide) { return to_divide/divisor } );
return transformed;
}
This code will only work on a compiler that understands lamdas - VC2008 and earlier don't so you'll have to use a separate class or function to do the fourth term of std::transform.
Cheers,
Ash
|
|
|
|
|
Time to whip out a lambda function...
Steve
|
|
|
|
|
Using a Lambda here was a bit pointless, I've been so completely wrapped up in saying "Coo, aren't lambda's wonderful?" that I'd completely forgotten about std::divide which is a fair bit easier to read.
Although they do get rid of the need to do the std::bind2nd, which can only be a good thing!
Cheers,
Ash
|
|
|
|
|
I don't know, many people who aren't use to functional programming and function objects find the bind2nd and divides stuff perplexing. Even if you're use to it, if the logic gets even moderately more complex it gets very arcane and the lamda expression will be a lot simpler. My concern is that barely any people would be using a compiler that supports them yet.
Steve
|
|
|
|
|
One advantage Lambdas have over a lot of other C++ features that have been introduced in the past is that two of the more mainstream compilers are supporting them before the C++0x standard's even been voted in. Compare that to the 5 year lag a certain compiler manufacturer had with the last standard and I'm hopeful they'll be mainstream fairly soon.
Having said that I'm depressed at the number of people using VC++ 6 on this board so maybe by 2020 we'll see the mainstream get there
Cheers,
Ash - who doesn't usually hijack threads like this
|
|
|
|
|
Thanks for your help Ash. I'm currently doing the following and it works! However, how could I send "op_divide" another parameter? I don't always want to divide by "5", I'd like to send "op_divide" the denominator to use.
long double op_divide (long double d) { return d/5; }
transform (vec1.begin(), vec1.end(), vec2.begin(), op_divide);
Thanks!
|
|
|
|
|
You're using a function with a hardcoded divisor in it, so the problem is how to get a second parameter into it.
If you want to keep using a function then you can expand your function to take a second parameter:
long double op_divide( long double numerator, long double denominator ) { return numerator/denominator; }
and use std::bind2nd to create a function object to pass to transform:
transform( vec.begin(), vec1.end(), vec2.begin(), std::bind2nd( op_divide, 5.0 ) );
[That's unchecked BTW, I'm not on a computer with a compiler on to check it - I'm pretty sure it will though.]
If you're doing that you could try using std::divides which does the same thing and saves writing the function.
Cheers,
Ash
|
|
|
|
|
Thanks Ash. I implemented the code, but it is giving many errors, the first being:
error C2825: '_Fn2': must be a class or namespace when followed by '::'
and the 2nd:
error C2039: 'first_argument_type' : is not a member of '`global namespace''
|
|
|
|
|
Ooops, forgot to add in the bit of special sauce to convert the binary function pointer to a function object:
std::transform( input.begin(), input.end(), output.begin(), std::bind2nd( std::ptr_fun( op_divide ), 5 ) ) );
What you've got is a function that takes two arguments. The std::ptr_fun function returns a function object, the function call operator of which passes the arguments to the contained function. The std::bind2nd returns another function object:
- the function call operator takes 1 parameter
- the constructor of the object stores another parameter
- the function call operator passes the 1 parameter and the stored parameter through to the contained binary function object which passes them through to the contained function pointer.
Arrrgggghhhhh... I want me Lambda's back!
Cheers,
Ash
PS: That's as clear as mud, I'll try and clean it up later
|
|
|
|
|
worked great, thanks Ash!
|
|
|
|
|
Here's one way:
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <functional>
int _tmain(int argc, _TCHAR* argv[])
{
using namespace std;
vector<double> nums;
for (int i=1; i<=10; ++i)
nums.push_back(i);
cout << "Before: ";
copy(nums.begin(), nums.end(), ostream_iterator<double>(cout, " "));
cout << endl << endl;
transform(
nums.begin(), nums.end(),
nums.begin(),
bind2nd(divides<double>(), 2.0)
);
cout << "After: ";
copy(nums.begin(), nums.end(), ostream_iterator<double>(cout, " "));
cout << endl << endl;
return 0;
}
Steve
|
|
|
|
|
|
Hi guys how can i accomplish this i need to show a do you want to update message every time the app is started the first time windows has rebooted.
Thanks!!
|
|
|
|
|
You could check for a 'flag' in some configuration file or the registry. After your app runs, set the flag to some other value (or remove it).
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
ok, but how do you detect that windows has reboot??, i mean i put a registry key called "doUpdate" value=false then i start my app i change that registry to true, so the next i launch my app it will not show the message that is ok, but how to reset that value when windows is restarted?
|
|
|
|
|
You might could take advantage of the HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run key by putting a small app there that runs the next time Windows is started.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|