M
ma740988
Consider this statement in Excel's text Thinking in C++, Vol 2:
/// 1
" A vector starts by grabbing a block of storage, as if it's taking a
guess at how many objects you plan to put into it. As long as you
don't try to put in more objects than can be held in the initial block
of storage, everything proceeds rapidly. (If you do know how many
objects to expect, you can preallocate storage using reserve().) "
So now:
typedef vector<int> VEC_INT;
VEC_INT value_vec;
So I create an instance of VEC_INT. capacity and size are both zeros.
That said, how do I detemine what size 'initial block of storage'
corresponds to? Trying to get a feel for the statements 'starts by
grabbing a block of storage' and 'intial block of storage'. Perhaps
I'm mistaken but neither max_size, capacity or size will help me
determine this 'initial block of storage', hence I'm confused.
/// 2
# include <iostream>
# include <vector>
# include <algorithm>
using namespace std;
typedef std:air<int, int> my_pair;
typedef std::vector<my_pair> vec_pair;
typedef vec_pair::iterator vec_pair_it;
int main()
{
typedef vector<int> VEC_INT;
VEC_INT value_vec;
value_vec.push_back(6);
value_vec.push_back(5);
value_vec.push_back(7);
value_vec.push_back(8);
vec_pair v;
v.push_back(std::make_pair(5, 0xA5));
v.push_back(std::make_pair(6, 0xB5));
v.push_back(std::make_pair(7, 0xC5));
v.push_back(std::make_pair(8, 0xD5));
int offset = 0;
for (size_t jdx(0); jdx < value_vec.size(); ++jdx)
{
size_t idx(0);
int val = value_vec[jdx];
for (; idx < v.size(); ++idx)
{
if (v[idx].first == val) {
offset = v[idx].second;
break;
}
}
// Now I use the offset (this piece not shown)
// later add current_payload to offset for next time
// NOTE: this is only an example. current_payload in real code
varies....
int current_payload = 0x1000;
v[idx].second += current_payload;
}
// check
vec_pair_it it = v.begin();
for (vec_pair_it it2 = v.begin(); it2 != v.end(); ++it2)
{
cout << " first " << it2->first << endl;
cout << " second " << it2->second << endl;
}
}
At issue.
1. I loop over the vector of pairs.
2. If v[idx].first == val. I store v[idx].second into a parameter
called offset.
The parameter offset is then passed to a member function (that's not
shown). Lastly I update v[idx].second by additing current_payload to
v[idx].second.
My question: Is there an alternate approach to items 1 and 2, that'll
use perhaps a function object or an algorithm? A revised
implementation that'll eliminate/rid the use of the for loop.
Note: in the real code current_payload is determined by the std::min
of two parameters - call them x and y. (i.e std::min (x, y). the
point being some things were omitted for simplicity.
Thanks in advance.
/// 1
" A vector starts by grabbing a block of storage, as if it's taking a
guess at how many objects you plan to put into it. As long as you
don't try to put in more objects than can be held in the initial block
of storage, everything proceeds rapidly. (If you do know how many
objects to expect, you can preallocate storage using reserve().) "
So now:
typedef vector<int> VEC_INT;
VEC_INT value_vec;
So I create an instance of VEC_INT. capacity and size are both zeros.
That said, how do I detemine what size 'initial block of storage'
corresponds to? Trying to get a feel for the statements 'starts by
grabbing a block of storage' and 'intial block of storage'. Perhaps
I'm mistaken but neither max_size, capacity or size will help me
determine this 'initial block of storage', hence I'm confused.
/// 2
# include <iostream>
# include <vector>
# include <algorithm>
using namespace std;
typedef std:air<int, int> my_pair;
typedef std::vector<my_pair> vec_pair;
typedef vec_pair::iterator vec_pair_it;
int main()
{
typedef vector<int> VEC_INT;
VEC_INT value_vec;
value_vec.push_back(6);
value_vec.push_back(5);
value_vec.push_back(7);
value_vec.push_back(8);
vec_pair v;
v.push_back(std::make_pair(5, 0xA5));
v.push_back(std::make_pair(6, 0xB5));
v.push_back(std::make_pair(7, 0xC5));
v.push_back(std::make_pair(8, 0xD5));
int offset = 0;
for (size_t jdx(0); jdx < value_vec.size(); ++jdx)
{
size_t idx(0);
int val = value_vec[jdx];
for (; idx < v.size(); ++idx)
{
if (v[idx].first == val) {
offset = v[idx].second;
break;
}
}
// Now I use the offset (this piece not shown)
// later add current_payload to offset for next time
// NOTE: this is only an example. current_payload in real code
varies....
int current_payload = 0x1000;
v[idx].second += current_payload;
}
// check
vec_pair_it it = v.begin();
for (vec_pair_it it2 = v.begin(); it2 != v.end(); ++it2)
{
cout << " first " << it2->first << endl;
cout << " second " << it2->second << endl;
}
}
At issue.
1. I loop over the vector of pairs.
2. If v[idx].first == val. I store v[idx].second into a parameter
called offset.
The parameter offset is then passed to a member function (that's not
shown). Lastly I update v[idx].second by additing current_payload to
v[idx].second.
My question: Is there an alternate approach to items 1 and 2, that'll
use perhaps a function object or an algorithm? A revised
implementation that'll eliminate/rid the use of the for loop.
Note: in the real code current_payload is determined by the std::min
of two parameters - call them x and y. (i.e std::min (x, y). the
point being some things were omitted for simplicity.
Thanks in advance.