C++ :: How To Store Data From A Class Into A Container
Feb 10, 2013I'm having trouble understanding on how to actually store into a container from another class. Any simple example.
View 9 RepliesI'm having trouble understanding on how to actually store into a container from another class. Any simple example.
View 9 RepliesI have a class I use to store column data read from delimited text files.
Code:
class column_data {
// class functions here
public:
// initialize class members
column_data()
: header(""),
[Code] ....
Each column is read into the vector of string. Next the string data is copy converted to the vector of double or the vector of int depending on the data type. There can also be additional transformations of the data (normalization, scaling, etc) and the transformed data is copied to the transformed_double_data vector. I do end up with multiple copies of the data, but I can clear some of the vectors if I don't need all the versions of the data anymore. Each column object goes into a container like a vector of objects or map of objects depending on how I need to access the column data.
There are functions that calculate statistics on the data in the columns and the results are assigned to the object variables like validate_r2. There are separate statistics for each column, so it seems logical to store the results in the column object. This works well for statistics like validate_r2 (pearson's correlation coefficient).
The current issue is that I have other statistics like "pw50" that I need to calculate and store. The statistic is the percent within +/- 50 units and the request for pw50 is passed in from the command line. The issue is that there could be several of these and they can have more or less any value like, pw50, pw75, pw100, etc. Storing data like this doesn't fit into my scheme very well because I can't pre-declare a variable to hold the result for every possible option. I don't know in advance how many, if any, of these there will be. I was thinking of adding two vectors, one of string and one of double,
Code:
class column_data {
public:
// initialize class members
column_data()
: header(""),
type(""),
use(""),
validate_r2(0.0),
[Code] ....
For each of this type of statistic requested, I would push_back a label into the string vector and then put the result into the parallel position in the double vector. This seems like I would work, but there are probably some things that could be done with pointers that might be better. Another option would be a vector of objects where each object had a string value for the label and a double for the result. That would be more bullet proof as far as keeping the label and value together, but I have not ever nested objects in a class like that so I thought I would post. I don't know the syntax for declaring an object of class_b inside the definition of class_a. The need is to provide dynamic storage for an unspecified number of these statistics and a reliable way to retrieve the values using the label.
I searching for a container/collection, that can access very fast to objects with the associated id.
Arrays are a bad idea, because it can be that I must store objects with a big id for example 9394034, so the array would be to big.
I have a Point class that's already implemented. My goal is to implement a container class called Line that holds the Point class. I'm not allowed to use any existing container classes for this. Here's what I'm working with:
//File: Point.h
#ifndef POINT_H_
#define POINT_H_
class Point {
public:
Point(int x = 0, int y = 0);
Point(const Point & t);
virtual ~Point() {};
[Code] ....
How I'm supposed to write Line.cpp...how do I access/add Points to Line without using something like a vector? I probably should've included what I've written so far.
#include "Line.h"
/**
* Default Line constructor
*/
Line::Line() {
[Code] ....
What is container class? what its advantage ?
View 2 Replies View RelatedI know that it's a class that contains data from another class I just do not know how to make them both work together.
View 1 Replies View RelatedI have a class called CTestObject and it has one variable int id. I want to add instances of this to an std container and be able to:
1. quickly determine if the value of "int id" exists in the container and update the object
or
2. add a new object to the container
Which std container is the best for this application? What method should I use to search the container for the object id?
How would I use the list container to hold a class?
class A {
private : int x;
public : void setX(int val) { x = val; }
};
class B {
private : std::list<A> pdata;
public : void addToList();
};
For adding, I thought of trying something like
void B::addToList() {
A *tmp = new A;
if(A != 0) {
tmp->setX(5);
pdata.insert(tmp);
delete tmp;
}
}
How would I do what I'm trying to do? Or is this the wrong way to go about it? For the actual program, "B" would contain several lists of various classes.
I need to find inverse of a matrix data in binary file should be used to form matrix.its not finding inverse correctly rest is working good.
Code:
#include <stdio.h>
void createbin();
void display();
void inverse();
int main()
{
createbin();
display();
inverse();
[Code] ....
I have written the following code to add data to text files which are required to store 3D scan data (I have to calculate the y-coordinate). My code seems to work except that it stops working when I want to create more than ten text files i.e. the directory I am trying to store them in will not hold any more than ten text files. Code is shown below.
#include <string>
#include <locale>
#include <iomanip>
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
[Code] ....
class A (abstract)
class B : A
class C {
void add ( A(&*?) a )
std::vector<std::unique_ptr<A>> data; //unique_ptr<A> because A is abstract and therefore vector<A> isn't possible
}
upper situation. What is the best way to pass add an object of class B to C?
with C::add(A* a){ vector.push_back( unique_ptr<A>(a) ); }
and
int main() {
C c;
c.add( new B() );
}
This works, but i don't think it's very nice, because you could delete the pointer in main. What happens then with the unique_ptr? I could probably make C::add( std::unique_ptr<A> u_p ); but maybe it can be avoided that the "user" (in main() ) has to create the unique_ptr itself.
struct Book{
string bookTitle;
string ISBN;
string author;
string publisher;
string dateAdded;
int qty;
double wholesale;
double retail;
};
What's the best way to store multiple of the above structure?Ideally, it would be with a binary output, but that isn't possible with the strings, unless I'm missing something. So that leaves me with two options.
-Create a second structure that has an outrageous char[] length, and then copy the data from the original structure to write/read to/from the file. The flaws I see in this is excess runtime with copying, and a larger amount of memory is temporarily needed. The positive I see is I can use binary output, which is easier.
-The second option is to just use an ASCII file, and put each element on it's own line, and then just use getline to read each line. The flaw I see in this is that I have to read the file into the structure, instead of just reading the structure and the file can be tampered with (Not a huge concern). The positive is the lack of negative from the above.
How data actually store in the File. Like do data store just in one line or at different places for example in array data store in adjacent memory block while in struct data store at different memory block.?
View 2 Replies View Relatedstart writing a program to store some information and then have it recalled. I would like to be able to store a list of the video games I own because i thought it would be fun :)
View 1 Replies View RelatedHow can I store this data into an array?And I only can read the first integer.. 10.... How can I read the whole file...Heres the data:
10 alice 4/23/1972 123-45-6789 support assistant
3 bob 6/7/1980 111-12-1134 logistics manager
1 carol 10/2/1963 987-123-1143 admin ceo
2 dave 10/3/1974 902-22-8914 admin cfo
17 erin 6/13/1991 126-83-1942 technology supervisor
15 frank 2/22/1987 303-12-1122 logistics assistant
Heres what I have so far?
#include <iostream> //Accesses libaries for console input and output
#include <fstream> //Needed to access the fstream object to read files
#include <string> //Needed to access the string class
#include <cstdlib>
[code]....
I have this data store into a data structure.
i j x y w h w*h
0 0 0 0 9 11 99
1 0 0 11 9 10 90
2 0 0 21 9 11 99
0 1 9 0 8 12 96
1 1 9 12 8 7 56
2 1 9 19 8 6 48
[Code]...
Code:
struct data {
//Here
/*! horizontal position */
int x;
/*! vertical position */
int y;
/*! width */
int w;
/
[Code]...
data and then i have an array /*! it contain group_id of each data line*/ int group_id[16]={0,0,0,0,3,3,1,1,1,3,3,2,2,2,2,1}; I never worked with 2D array before. My problem is that i want to create a 2D array of that data for example if i write data[j][i].. It will give me the reference/value of all data lines that belongs to j column, and same with group_id[j][i]. I don't know how i can store these structure vaules in this like 2D array.
I wanna store my data by descending order auto without doing a classification after , i have come up with a code but it crashes.
Code:
void list::create(int num) {
struct node * n,*temp;
n=new node;
n->data=num;
if(head==NULL)
[Code]....
I don't wanna use the list library with the push.buck, i need to do it by coding it without fix functions ....
How I would store all the data. Also how would I access it after I store it. Side Note we are not allowed to use structures or objects.
For the purposes of this gradebook you should first provide a menu with the following options
-Add a new course Add a new student to a course Add grades for a student in a course Print a list of all grades for a student in a course Print a list of all students in a course Compute the average for a student in a course Compute the average for a course Store Gradebook (to a disk file) Load Gradebook (from a disk file)
-Each of these menu items should correspond to a function you will write.
-For the basic program each student will be represented by an ID number And each course by a course number
-Grades will be whole numbers only (no fractional part)
As indicated in the menu you will need to store and load using a disk file so that the data is retained.
Here are so limiting values in defining your data structures: Maximum Number of students (total) 100 Max number of courses 25 Max number of courses per student 4 Max number of grades per student per course 10
That;s what i have so far: problem: the output data is not correct.
input file
1301 105515018 "Boatswain" "Michael R." CSE 230 ="R01"
1301 103993269 "Castille" "Michael Jr" CSE 230 ="R03"
1301 103993267 "Castille" "Janice" CSE 230 ="R03"
[Code]....
I serialize a XML file, and i have this values here:
public class ServiceConfig {
public List<DatabaseDescriptor> Databases { get; set; }
} public class DatabaseDescriptor {
[XmlElement("Name")]
[Code] ....
I have a form1 which is a datagrid view and get's updated like this:
dataGridView1.DataSource = xmlData.Databases;
Now i have a button: get tables, and it opens up form 2, there it's supposed to appear all the tables of the selected Database, when i click on the row i get the database name so i get all the list of Tables from
DatabaseDescriptor
where
DatabaseDescriptor.Name == name
(I get this when i select the row)
But then on the 2nd form, i also have 2 textbox so i add a new table, i add the tablename and id. the new value should appear in the datagridview of the tables, and when i close it and reopen it, it should be there. Maybe i should concatenate first the values or something, how do i do?
i need to create a new integer data type called BigInt to store a big big integer, which includes Dint(8 bytes) and Qint(16 bytes)
here is the hint/
typedef struct BigInt {
Int data[2];
}
How can i "scanf" and "printf" them????
void ScanBigInt(const char *format, BigInt &x)
if format is “%dd” -> input Dint, if format is “%qd”--> input Qint
void PrintBigInt(const char *format, BigInt x)
How is it possible to store the related data of a program C++ into an external file (like as .txt) and retrieve it?
I want to write c++ program that can contain data members " id and phone"
like as
Code:
class profile {
private: int id,phone;
public:
void retriever() {
cout<<"Enter your ID: ";
cin>>id;
cout<<"Your Phone number is "<< phone;
};
}
avoiding formats in above class, purpose is to store data already in txt file with different id and phone numbers and it is to be retrieved by matching id number to txt file and viewing its related phone number.
I sent 1preample and 4bytes in transmitting side , preample contains only 1 and 0 with 4milliseconds delay, each byte contains 8bits with 2milliseconds delay , if a bit contains 1 it will send 1millisecond high and 1millisecond low signal ,and if a bit contains 0 it will send 2millisecond continuous delay, I have a problem at receiving side how to capture and store the preample and 4byte values in a buffer , i am using ARM processor it will update every 5microsecond ..
View 1 Replies View RelatedOverview of problem : I am using std::vector to hold objects of Subject. Now this vector contains lots of objects( with lots I mean 10-20 objects at max) . These objects have string member values like category and sub_category. Both category and sub_category can have string which can be same of other objects's sub_category & category.
Issue: Now I want my std::vector to have only those objects whose's sub_category are unique. If category is not unique that's not a problem .
Secondly if we found 2 objects having same sub_category then we have to delete one of them from the vector. we will delete it based on some rules example
Rules for deleting are if
i) instance of Subject ->category = " Land " OR if category = "Jungle" then delete other duplicate object ,
ii) if above condition doesn't match then delete either of them.
I am wondering , how would I compare the sub-items from the vector . For example. I have class say Subject
class Subject {
public :
// some constructors,
// functions to get ., set category and sub category
std::String get_sub_category()
std::string get_category();
private:
std::string category;
std::string sub_category;
}
I have vector which stores object of Subjects. Example : vector<Subject> copy_vector;
Now what I want is to delete the object from vector that has same sub_category I am not looking for source code buT i need a starting point,? Example:
copy_vector[0] = Animal object that has sub_category Tiger
copy_vector [1] = Animal object with Lion as sub category
copy_vector[2] = Forest object with sub_category Tiger
What I want is to based on some conditions(which I can do ) remove either Forest or Animal object containing Tiger. But for that how would I do comparison? I have written the function and have checked it.
std::vector< Subject >copy_vector;
// copy_vector contains all the objects of Subject with redundant sub_category
for( std::vector< Subject >::iterator ii = copy_vector.begin() ; ii != copy_vector.end() ; ++ii ) {
sub_category = ii->get_sub_category();
[code] ....
The program is supposed to read a formatted .txt file and store the data into their respective [Class] Members. It will then output the data collected onto screen.I overloaded ifstream and istream. When I call for file>>ptr[i] to read the data, something goes wrong. It prompts me to input data (which is in istream overload).The Data in .txt file is as follows;
#ifndef STUDENTTESTSCORES_H
#define STUDENTTESTSCORES_H
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
[code]....
Read the input file of data ( employees.txt) and store them in arrays. This company can have up to 55 employees [b]i need to do these following in these program:
Write a function to read the input file and store the data in arrays.
Write a function to calculate regular pay.
Write a function to calculate overtime pay
Write a function to calculate gross pay.
Write a function to bubble sort the employees into order by last name, first name.
Write any swap functions that are needed.
Write a function to write output to a file called payroll.txt
Format of file is EMPLOYEES.TXT[/b]
Hours Pay Rate Employee Number First Name Last name
40.0 10.00 A1234 Jane Adams
50.0 10.00 L8765 Mary Lincoln
25.5 10.85 W7654 Martha Washington
52.0 15.75 A9876 John Adams
45.0 25.00 W1235 George Washington
40.25 55.00 L9087 Abraham Lincoln
30.0 9.75 T9876 William Tell
42.5 12.50 M7654 Missy Muffett
30.0 10.00 P8765 Peter Piper
HERE IS MY CODE SO FAR:
#include <iostream>
#include <cstdio>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
void tellUser();
[code]....