473,402 Members | 2,053 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,402 software developers and data experts.

how do I access a member of vector when this vector is a member of a class

I am a beginner. So this question could be very stupid.

Would anyone help me to solve this problem? A formatted txt file is
read. Then i need to look into a vector who is a member of a class to
search the matched string and return this string. For example, this
vector has (string1, string2, ...stringn). When the formatted txt file
is read, I get string j which could be a member of the above vector.
This vector is a private member of class obj. Please help me to build
a function in the main and get stringj from that vector.

thanks
Jul 19 '05 #1
8 2066

"ding feng" <d.******@lboro.ac.uk> wrote in message
news:42**************************@posting.google.c om...
I am a beginner. So this question could be very stupid.

Would anyone help me to solve this problem? A formatted txt file is
read. Then i need to look into a vector who is a member of a class to
search the matched string and return this string. For example, this
vector has (string1, string2, ...stringn). When the formatted txt file
is read, I get string j which could be a member of the above vector.
This vector is a private member of class obj. Please help me to build
a function in the main and get stringj from that vector.

thanks


If you need to access private members of a class, then you should write a
function in that class that will search its own vector for the given string.
(I don't see why you would want to return the string, though, since it's the
same as what you asked the function to look for. Probably better to return
an index or iterator.)

(By the way, I assume when you wrote "build a function in the main" that you
meant "...in the main unit, outside the class", and not "...in the function
main()". You can't put a function inside another function in C++.)

So, you just need to add a Find( string str ) function in your class. That
function can do the search, and can use the vector's own find function to do
it. Look up find for the vector template class. (I think that's what it's
called...?)

-Howard
Jul 19 '05 #2
ding feng wrote:

I am a beginner. So this question could be very stupid.

Would anyone help me to solve this problem? A formatted txt file is
read. Then i need to look into a vector who is a member of a class to
search the matched string and return this string. For example, this
vector has (string1, string2, ...stringn). When the formatted txt file
is read, I get string j which could be a member of the above vector.
This vector is a private member of class obj. Please help me to build
a function in the main and get stringj from that vector.

thanks


you can create a public method for your obj class which takes
a string as an argument and returns what ever you want ie boolean i.e.

class obj
public:
bool search_vector_method(string& str)
{
//Include what Dhruv replied

if(vi != v.end())
{
return true;
}
else
{
return false;
}
}

//then in main create string to search for

string str2match;
cin >> str2match;

//create an object from the obj class
//and call the public method

obj obj1;
if(obj1.search_vector_method(str2match) == "no_match")
{
.....
}
else
{
.....
}

Something alonmg those lines - hope it helps!
Jul 19 '05 #3
Graham Cox wrote:

Whoops - small correction in last if statment!

ding feng wrote:

I am a beginner. So this question could be very stupid.

Would anyone help me to solve this problem? A formatted txt file is
read. Then i need to look into a vector who is a member of a class to
search the matched string and return this string. For example, this
vector has (string1, string2, ...stringn). When the formatted txt file
is read, I get string j which could be a member of the above vector.
This vector is a private member of class obj. Please help me to build
a function in the main and get stringj from that vector.

thanks


you can create a public method for your obj class which takes
a string as an argument and returns what ever you want ie boolean i.e.

class obj
public:
bool search_vector_method(string& str)
{
//Include what Dhruv replied

if(vi != v.end())
{
return true;
}
else
{
return false;
}
}

//then in main create string to search for

string str2match;
cin >> str2match;

//create an object from the obj class
//and call the public method

obj obj1;
if(obj1.search_vector_method(str2match))
{
....
}
else
{
....
}

Something alonmg those lines - hope it helps!

Jul 19 '05 #4

"ding feng" <d.******@lboro.ac.uk> wrote in message
news:42**************************@posting.google.c om...
I am a beginner. So this question could be very stupid.

Would anyone help me to solve this problem? A formatted txt file is
read. Then i need to look into a vector who is a member of a class to
search the matched string and return this string.
If you got one string and try to find a matching string why would you want
to return that string which you already have?
For example, this
vector has (string1, string2, ...stringn). When the formatted txt file
is read, I get string j which could be a member of the above vector.
This vector is a private member of class obj. Please help me to build
a function in the main and get stringj from that vector.

thanks


What you need is a member function that can access the private members and
perform the search:

class CMyClass {
public:
bool FindString( const string& Str );
...
private:
vector<string> m_Data;
};

bool CMyClass::FindString( const string& Str )
{
vector<string>::iterator Iter = find( m_Data.begin, m_Data.end() );
if ( Iter == m_Data.end() )
return false;

return true;
}

HTH
Chris
Jul 19 '05 #5

"Dhruv" <dh*******@gmx.net> wrote in message
news:pa****************************@gmx.net...
On Fri, 27 Jun 2003 06:04:41 -0700, ding feng wrote:
[SNIP> There you go!!!!!!!!!!!!
Sorry pal, but the way you go is not the way the OP wants to go! He wanted
to know how to access the vector which is a private member of an object from
the outside!
HTH,
-Dhruv.
My C++ wish list: A vector that has as its 2nd parameter a stream object
instead of an allocator, so that you can pass it any stream, and it uses
that as storage. Then, you can very conveniently provide a memory stream,
which will work like an allocator!!!


An what if the user passes cout or cin which are perfectly fine streams?

Regards
Chris
Jul 19 '05 #6
[SNIP>
There you go!!!!!!!!!!!!


Sorry pal, but the way you go is not the way the OP wants to go! He
wanted to know how to access the vector which is a private member of an
object from the outside!


Ok, then just put the code in a member function.

My C++ wish list: A vector that has as its 2nd parameter a stream
object instead of an allocator, so that you can pass it any stream, and
it uses that as storage. Then, you can very conveniently provide a
memory stream, which will work like an allocator!!!

An what if the user passes cout or cin which are perfectly fine streams?


cout and cin are objects of a stream type, not stream classes.
I get what you are asking for.

Then write and read to and from the streams, which would be the
behaviour...... Also, the stream needs to support <<, and >>.

So, we get something like this:

vector<type, stream_name>, so:

vector<int, console_stream> v1; //vector will do ONLY output to the stream in terms
of only ints. That's because only << is defined for cout. If WE write to
the stream, we have to only cout<<int. Otherwise, the stream for the
vector will get corrupted.

Now, we wish to write 2 integers to the screen:

v1.push_back (23);
v1.push_back (56);

If we wish to write these ints to a file, we just do:

vector<int, file_stream> v1;
//and same code goes here.

This will be more useful in file streams, where <<, and >> are both
defined.

Regards,
-Dhruv.



Jul 19 '05 #7

"Dhruv" <dh*******@gmx.net> wrote in message
news:pa****************************@gmx.net...
[SNIP]

An what if the user passes cout or cin which are perfectly fine streams?

cout and cin are objects of a stream type, not stream classes.


Sorry, of course. I just missed that you were refering to the second
template parameter and not to the ctor.
I understand your motivation for using streams as a kind of allocators
because you'd get a nice persistency framework but there are subtle issues
which make life quite hard.

I get what you are asking for.
[SNIP] we wish to write 2 integers to the screen:

v1.push_back (23);
v1.push_back (56);

And what will the following code do which is fine with the common vector
behavior?

for( vector<int, console_stream>::iterator Iter _ v1.begin(); Iter !=
v1.end(); ++Iter ) {
// do whatever you want with Iter
}

To access values pushed into vector<int, console_stream> they must be stored
somewhere so we'd have to have a "normal" storing vector and additionally
the ability to output to the screen.

If we wish to write these ints to a file, we just do:

vector<int, file_stream> v1;
//and same code goes here.

This will be more useful in file streams, where <<, and >> are both
defined.
That would be really nice but in principle you can do this with the help of
ifstream/ofstream iterators. The tricky thing with your solution is what
should be done if one wants to store pointers in the vector. If you write
them to a file & restore them afterwards they're not valid anymore. Thus all
the issues regarding persistency will come down on the one who has to
implement this behavior.

Don't get me wrong your idea is nice but the problem is that it would
introduce a whole new behavior which is not necessarily compatible to the
normal vector behavior. Most people consider it a good design rule that one
shouldn't pack many special cases into one "almighty" tool but to
differentiate. IMHO the stream iterators in combination with the collection
templates should suffice to solve what you want, respectively you can still
wrap them in your own framework so that the user doesn't need to know about
the details.

Regards,
-Dhruv.


Best regards
Chris
Jul 19 '05 #8
On Sat, 28 Jun 2003 17:57:55 +0200, Chris Theis wrote:

"Dhruv" <dh*******@gmx.net> wrote in message
news:pa****************************@gmx.net...
[SNIP]
>>
> An what if the user passes cout or cin which are perfectly fine streams?

cout and cin are objects of a stream type, not stream classes.


Sorry, of course. I just missed that you were refering to the second
template parameter and not to the ctor.
I understand your motivation for using streams as a kind of allocators
because you'd get a nice persistency framework but there are subtle issues
which make life quite hard.

Yes, it would quite make life hard for the developer, but the user would
enjoy!!!!!! I have thought about it, but it seems that it needs much more
thought than I initially anticipated...... However, I've got to learn
about streams first.

I get what you are asking for.

[SNIP]
we wish to write 2 integers to the screen:

v1.push_back (23);
v1.push_back (56);


And what will the following code do which is fine with the common vector
behavior?

for( vector<int, console_stream>::iterator Iter _ v1.begin(); Iter !=
v1.end(); ++Iter ) {
// do whatever you want with Iter
}

Yes, this has occured to me, I'll geive it some more thought as I said.
To access values pushed into vector<int, console_stream> they must be stored
somewhere so we'd have to have a "normal" storing vector and additionally
the ability to output to the screen.

If we wish to write these ints to a file, we just do:

vector<int, file_stream> v1;
//and same code goes here.

This will be more useful in file streams, where <<, and >> are both
defined.
That would be really nice but in principle you can do this with the help of
ifstream/ofstream iterators. The tricky thing with your solution is what
should be done if one wants to store pointers in the vector. If you write
them to a file & restore them afterwards they're not valid anymore. Thus all
the issues regarding persistency will come down on the one who has to
implement this behavior.

But, no one would use persistant storage for pointers.
Don't get me wrong your idea is nice but the problem is that it would
introduce a whole new behavior which is not necessarily compatible to the
normal vector behavior. Most people consider it a good design rule that one
shouldn't pack many special cases into one "almighty" tool but to
differentiate. IMHO the stream iterators in combination with the collection
templates should suffice to solve what you want, respectively you can still
wrap them in your own framework so that the user doesn't need to know about
the details.


Some more thought is on the cards for me. (including some more sleep).

Regards,
-Dhruv.

Jul 19 '05 #9

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

Similar topics

8
by: Bruce | last post by:
OK, this won't compile saying it can't access private members declared in class F. I don't get it and even if I make the entire class public, it still says that. I realize it has something to do...
15
by: cppaddict | last post by:
I have class with two static member objects, one of type int and one of type vector<int>. static int myStaticMemberInt static vector<int> myStaticMemberVector; I know how to initialize the...
3
by: Mark Turney | last post by:
Problem: I have a vector full of two different derived class objects (class B and class C) that are derived from the same base class A. I want to loop through vector and invoke a member function...
4
by: Serge | last post by:
Hi, I have no problem creating a static member variable with integers, etc but when I try the same with a vector then I always get linker errors that the static member variable is unknown...
2
by: gvdeynde | last post by:
Hi, What's the "best" way to have an object ObjA of class A have read-access (but not write-access) to a data member of object ObjB of class B. Class B has a std::vector<T> as a datamember. ObjA...
5
by: Pedro Sousa | last post by:
Hi, I'm trying to create an template class that represent points in all possible dimensions, what I've made until now is #ifndef _POINT_HPP_ #define _POINT_HPP_ #include <vector>
5
by: andrewmorrey | last post by:
Hello, I've got a VC++ project containing multiple classes and a main function. In one of the class functions, it reads from a text file and places the data into a vector; //...
4
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
31
by: huili80 | last post by:
Say I have two classes: class A { public: int x; }; class B {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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,...

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.