473,387 Members | 1,535 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,387 software developers and data experts.

Use a class defined vector to store "paired" data in each element

542 512MB
The exercise from my book reads:
'Define a class named Name_value that holds a string and a value (int)and a vector named Name_value. Give it a constructor. Store name and value "pairs" in each vector element instead of using two separate vectors... as were
used in ex 4_19.' The following is the debris left from my many previous attempts. I would appreciate some specific direction on how to proceed as I'm stumped. I have Googled extensively without joy. There is no urgency.
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<vector>
  3. using namespace std;
  4.  
  5. class Name_value
  6. {
  7. public:
  8.        string name;
  9.        int value;
  10.        Name_value (vector<Name_value>v);      
  11.  
  12.  
  13. };      
  14.  
  15. int main()
  16. {
  17. Name_value  v;           
  18.  
  19. system("pause");
  20. return 0;
  21. }
Jul 24 '10 #1
4 1775
Banfa
9,065 Expert Mod 8TB
I think this is a very poorly worded question and I suspect it means

Define a class named Name_value that holds string and value (int) pairs using a vector named Name_value. Give it a constructor and a copy constructor. Store name and value "pairs" in each vector element instead of using two separate vectors.

However this would end up just being a very simple wrapper for a vector storing the relevant type, i.e. no real need for this class.
Jul 24 '10 #2
whodgson
542 512MB
Thanks Banfa..Yes I think you are quite correct with your restatement of the question(but I can`t comment on the need for a copy constructor). Are you saying that it is not possible to write some code that achieves the basic requirement of storing two different (primitive) types under one class name (type) into one vector element?
Jul 25 '10 #3
Banfa
9,065 Expert Mod 8TB
No It is possible, I am saying that vector does such a good job there seems little point wrapping another class round the vector.

Instead of

Expand|Select|Wrap|Line Numbers
  1. class Name_value
  2. {
  3. public:
  4.     string name;
  5.     int value;
  6.     Name_value (vector<Name_value>v);      
  7. };
  8.  
I personally would have

Expand|Select|Wrap|Line Numbers
  1. struct Name_value
  2. {
  3.     string name;
  4.     int value;
  5. };
  6.  
I know classes and structs are the same thing but I tend to use struct where the data is public especially if there are not methods on the structure (i.e. where a classic C structure can be used).

Then

Expand|Select|Wrap|Line Numbers
  1. std::vector<Name_value> Name_value;
  2.  
Appears to solve the question, no need for a wrapper class.
Jul 25 '10 #4
whodgson
542 512MB
Thanks Banfa. Will do some work using your approach.
Jul 26 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Anthony Baxter | last post by:
To go along with the 2.4a3 release, here's an updated version of the decorator PEP. It describes the state of decorators as they are in 2.4a3. PEP: 318 Title: Decorators for Functions and...
3
by: John Dunlop | last post by:
(Note crosspost and follow-ups to ciwah.) Nicolas Keller wrote in thread "Differences in form handling btw Mozilla and IE?": > The problem: I'm using a form that submit's (POST) its data via...
11
by: koperenkogel | last post by:
Dear cpp-ians, I am working with a vector of structures. vector <meta_segment> meta_segm (2421500); and the structure look like: struct meta_segment { float id; float num;
5
by: Mark Hannon | last post by:
I have been researching the "disabled" property for form data and wanted to know: - Does the disabled property supress the form item from being submitted? - Can the disabled property be applied...
15
by: Barry | last post by:
Hi group, Does anyone know why I get a W3 validator error (click the validation link at the bottom of the page) for http://www.polisource.com/consumer-protection.shtml saying "Line 227, column...
2
by: repairman2003 | last post by:
I'm trying to keep track of two objects, a vector of strings and a user defined date, ints set up in a struct. Having some trouble with it keeping track of the information though. //Defined as...
8
by: abcd | last post by:
I can get the value on the form at the server side by using Request.form("max") when max field is disabled I dont get value. For GUI and business logic purpose I have disabled some fields with...
5
by: evlong | last post by:
My question is about the STL map class and how it sorts data that it stores. By default (as far as I know) it sorts keys in ascending order or lexicographically. If I were to do something like...
9
by: Cao Yi | last post by:
Hi, here's a fract of codes, and what's the line "scanf("%lf%*", &cvi)" doing? ============================= do { printf("\nCoefficient: "); scanf("%lf%*", &cvi); getchar(); } while (cvi <=...
4
by: boheman | last post by:
Hi, I am wondering if there is a simple and quick way to return the indices of sorted vector. for example, I have a vector<intx containing {5, 2, 3, 0, 2}. I can use sort(x.begin(), x.end(),...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...

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.