473,405 Members | 2,261 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

How to make the vector size unchanged but values not?

Dear All,

I have a question. Assume

#include <vector>
using namespace std;

class A
{
private:
vector<intv;
int n;

public:
void Reset(int i)
{
n = i;
v.resize(i);
}

vector<int>& Vector()
{
return v;
}
};

For class A, 'n' and the size of 'v' must be always consistent. Every
time n is changed, v is reset with the size n. If n is not changed,
the vector v's size can not change, but its values can change, such as

A::Vector()[2] = 3;

But user can use A::Vector().resize() to change the size of the
vector. How can I avoid it and still keep all the std::vector 's
functions to change the values?

Thanks and best regards,

Shuisheng
Sep 19 '08 #1
4 1908
shuisheng wrote:
Dear All,

I have a question. Assume

#include <vector>
using namespace std;

class A
{
private:
vector<intv;
int n;

public:
void Reset(int i)
{
n = i;
v.resize(i);
}

vector<int>& Vector()
{
return v;
}
};

For class A, 'n' and the size of 'v' must be always consistent. Every
time n is changed, v is reset with the size n. If n is not changed,
the vector v's size can not change, but its values can change, such as

A::Vector()[2] = 3;

But user can use A::Vector().resize() to change the size of the
vector. How can I avoid it and still keep all the std::vector 's
functions to change the values?
The easy way is to ditch the variable i. Since it cannot be observed from
the outside, it does not make any sense to carry it around. Probably this
possibility is due to oversimplification of the problem and does not arise
in the original setting.

The standard way would be to not expose the internal vector by means of a
Vector() method but to narrow down its interface and make the needed
functions available through forwarding. They would also update i.

The hard way would be to not return a reference in the Vector() method, but
a smart reference that updates i upon destruction. Since we cannot overload
the dot-operator, there are some limitations to this method. We can,
however, return a smart pointer.
All in all, you seem to have a design problem that is best solved by
avoiding the problem. What is the underlying conundrum that you are
struggling with?
Best

Kai-Uwe Bux
Sep 19 '08 #2
On Sep 19, 10:34*am, Kai-Uwe Bux <jkherci...@gmx.netwrote:
shuisheng wrote:
Dear All,
I have a question. Assume
#include <vector>
using namespace std;
class A
{
private:
* vector<intv;
* int n;
public:
* void Reset(int i)
* {
* * n = i;
* * v.resize(i);
* }
* vector<int>& Vector()
* {
* * return v;
* }
};
For class A, 'n' and the size of 'v' must be always consistent. Every
time n is changed, v is reset with the size n. If n is not changed,
the vector v's size can not change, but its values can change, such as
A::Vector()[2] = 3;
But user can use A::Vector().resize() to change the size of the
vector. How can I avoid it and still keep all the std::vector 's
functions to change the values?

The easy way is to ditch the variable i. Since it cannot be observed from
the outside, it does not make any sense to carry it around. *Probably this
possibility is due to oversimplification of the problem and does not arise
in the original setting.

The standard way would be to not expose the internal vector by means of a
Vector() method but to narrow down its interface and make the needed
functions available through forwarding. They would also update i.

The hard way would be to not return a reference in the Vector() method, but
a smart reference that updates i upon destruction. Since we cannot overload
the dot-operator, there are some limitations to this method. We can,
however, return a smart pointer.

All in all, you seem to have a design problem that is best solved by
avoiding the problem. What is the underlying conundrum that you are
struggling with?

Best

Kai-Uwe Bux
The underlying problem is like that: I have a 2-D (two dimensional)
field (or array), the field points are arranged regularly along x and
y axis, which looks like a grid as follows,

y2 . . . .
y1 . . . .
y0 . . . .
x0 x1 x2 x3

where the dots denote field points. Given two vectors: xv and yv, the
grid points will be well defined. And the 2-D field have a size of
(size of xv) by (size of yv). So every time reset xv and yv, the size
of 2-D field will changed. But if the xv and yv do not change, only
the field values can change. For 2-D filed/array, I use blitz::array,
which is similar to std::vector but is 2-D.

Thank you for your help!
Sep 19 '08 #3
shuisheng wrote:
On Sep 19, 10:34*am, Kai-Uwe Bux <jkherci...@gmx.netwrote:
>shuisheng wrote:
Dear All,
I have a question. Assume
#include <vector>
using namespace std;
class A
{
private:
vector<intv;
int n;
public:
void Reset(int i)
{
n = i;
v.resize(i);
}
vector<int>& Vector()
{
return v;
}
};
For class A, 'n' and the size of 'v' must be always consistent. Every
time n is changed, v is reset with the size n. If n is not changed,
the vector v's size can not change, but its values can change, such as
A::Vector()[2] = 3;
But user can use A::Vector().resize() to change the size of the
vector. How can I avoid it and still keep all the std::vector 's
functions to change the values?

The easy way is to ditch the variable i. Since it cannot be observed from
the outside, it does not make any sense to carry it around. *Probably
this possibility is due to oversimplification of the problem and does not
arise in the original setting.

The standard way would be to not expose the internal vector by means of a
Vector() method but to narrow down its interface and make the needed
functions available through forwarding. They would also update i.

The hard way would be to not return a reference in the Vector() method,
but a smart reference that updates i upon destruction. Since we cannot
overload the dot-operator, there are some limitations to this method. We
can, however, return a smart pointer.

All in all, you seem to have a design problem that is best solved by
avoiding the problem. What is the underlying conundrum that you are
struggling with?

Best

Kai-Uwe Bux

The underlying problem is like that: I have a 2-D (two dimensional)
field (or array), the field points are arranged regularly along x and
y axis, which looks like a grid as follows,

y2 . . . .
y1 . . . .
y0 . . . .
x0 x1 x2 x3

where the dots denote field points. Given two vectors: xv and yv, the
grid points will be well defined. And the 2-D field have a size of
(size of xv) by (size of yv). So every time reset xv and yv, the size
of 2-D field will changed. But if the xv and yv do not change, only
the field values can change. For 2-D filed/array, I use blitz::array,
which is similar to std::vector but is 2-D.
Hm, the least invasive I can think of, is to split the setter/getter
function in two:

vector< int const & get_x ( void ) const {
return xy;
}

void set_x ( vector< int const & val ) {
xy = val;
// do whatever is needed to update the 2d-array
}

// similarly for y.

However, getter and setter methods still smell NotSoGood(tm). The preferred
way of going about this is to make the vectors and the 2d-array strictly
private members of the class and to devise an interface that describes how
an object of that type responds to messages. That way, proper encapsulation
is ensured. That, however, will require a good deal of knowledge about the
application domain where this class will be used.
Best

Kai-Uwe Bux
Sep 19 '08 #4
On Sep 19, 6:55*pm, shuisheng <shuishen...@yahoo.comwrote:
On Sep 19, 10:34*am, Kai-Uwe Bux <jkherci...@gmx.netwrote:
[...]

For 2-D filed/array, I use blitz::array,
which is similar to std::vector but is 2-D.
You can use splices for emulating 2D arrays.

Sep 20 '08 #5

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

Similar topics

1
by: TF | last post by:
I have a fixed array of vectors like: vector<string> a_vStr; Then I grow each vector item and use it later. Everything works fine but I am curious about following potential problem. When we...
5
by: Steve Hill | last post by:
Hi, suppose I have a vector allocated on the heap. Can I use a temporary (stack based vector) and swap to clear it, or is this unsafe. e.g. vector<int> *v; vector<int> tmp; v->swap(tmp); // is...
10
by: Stefan Höhne | last post by:
Hi, as I recon, std::vector::clear()'s semantics changed from MS VC++ 6.0 to MS' DOT.NET - compiler. In the 6.0 version the capacity() of the vector did not change with the call to...
1
by: robk | last post by:
Hi, Could someone know what is wrong with my code. First of all what I'm trying to do. I have (as can be seen) declared typedef's of vector STL. I want each value in vector which is part of...
5
by: Billy Patton | last post by:
I have a polygon loaded into a vector. I need to remove redundant points. Here is an example line segment that shows redundant points a---------b--------c--------d Both b and c are not...
10
by: Bob | last post by:
Here's what I have: void miniVector<T>::insertOrder(miniVector<T>& v,const T& item) { int i, j; T target; vSize += 1; T newVector; newVector=new T;
8
by: Alex Vinokur | last post by:
What is relation between std::vector's reserve() and erase()/clear()? vector<int> v; v.reserve(100); v.resize(100); v.erase(v.end()); How many elements are reserved here: 100 or 99?
42
by: Martin Jørgensen | last post by:
Hi, I'm trying to move a matlab program into c language. For those who knows matlab, this is the line I want to program in c: hx(1:nx,1:ny) = 0; % nx=10, ny=10 It works on a 2-dimensional...
9
by: Jess | last post by:
Hello, I tried to clear a vector "v" using "v.clear()". If "v" contains those objects that are non-built-in (e.g. string), then "clear()" can indeed remove all contents. However, if "v"...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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,...
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...
0
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...

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.