C++ :: Storing Constant - Frequently Accessed Data
Sep 9, 2013
I'm redesigning some code and I'm wondering what the best ways to store and access certain data is. The characteristics are as followed:
1) Based on data from a file, a distance matrix (N x N) is calculated.
2) Once the matrix is complete, the data will never change (unless a new file is read, but I can work around that by iteratively calling the problem with a new datafile on as command line parameter).
3) The data from the matrix is accessed billions of times from pretty much every other line of code.
In my old version, I had a class "Data" which a sub-class "Data::Distance" and I would put a reference in every other class that needed it. Now, my class hierarchy will be much flatter (basically all logic will be in one class; other classes will be POD structs).
Given the characteristics of the Distance table, is there a way to store them in a very efficiently-accessible way? Does it matter if it's stored in the main class where all the action happens in contrast to being a different class? Does making it static improve the performance? Casting it to const? Anything?
Again, the data is accessed billions of times so even minor differences can save a lot of time.
View 10 Replies
ADVERTISEMENT
Apr 24, 2014
Here in below code, the private data member is accessed directly using dot operator IN COPY CONSTRUCTOR and the program runs without error.
#include <cstdlib>
#include <iostream>
using namespace std;
class array {
int *p;
int size;
public:
array(int sz)
[code]....
View 1 Replies
View Related
Jan 8, 2014
I have defined a pointer ptr2 as follows:
const void * ptr2 ( new A(20) );
In this case I expect the data pointed by ptr2 must be constant. But when running the program, the data could still be changed successfully. Why ?
Below is the program:
#include <iostream>
using namespace std;
[URL] ....
class A {
public:
A( int ID ) {
cout << "--Constructor with ID=" << ID << endl;
this->ID = ID;
[Code] .....
The output:
--Constructor with ID=20
ID of ptr2 is 20
ID of ptr2 is 21 after changed!
-~Destructor with ID=21
End of main()
Process returned 0 (0x0) execution time : 0.002 s
Press ENTER to continue.
Question: What is the significance of "const" in const void * ?
View 19 Replies
View Related
Apr 9, 2014
Here's a part of my program. What I need to know is how I can pass an argument to the Book constructor so I can change the const data member Category (with cascading capacity if possible. I also posted some of my set functions for further comprehension.
class Book {
friend void CompPrice(Book &,Book&);
//friend function that has access to the member functions of this class
//The arguments sent to it are by address, and of type the class Book, so that it can have access to its member functions
private:
//private data members
[Code]...
View 1 Replies
View Related
Dec 9, 2014
I'm using const_cast to change the data of the constant variable. but on next while i'm trying to print the value its showing old data stored.
#include <iostream>
using namespace std;
int main() {
const int a = 5;
const_cast<int &>(a) = 6;
cout << "Hello World " << a << endl;
return 0;
}
Output : Hello World 5
why the value is getting changed again.?
View 4 Replies
View Related
Dec 21, 2012
Programe #1
// file.h
class File {
public:
static const int var = 9;
};
[Code]....
Program#1 is running fine, but program#2 gives linker error:
error LNK2005: "int GlobalVar" (?x@@3HA) already defined in file.obj
I know the header files are never compiled. Then in the above case, how the compiler knows the definition of variable var, but not able to find the definition of GlobalVar? What is the difference between this two programs?
View 3 Replies
View Related
Mar 14, 2013
I have to write a c++ program to read html code and do bunch of stuff. One thing that i have to do is to count the no of attributes and sort them in descenting out and print out only first 10 frequently used attributes. I counted them using maps and sorted them using multimaps but now dnt knw how to print only 10 elements
for(std::map<string, int>::iterator it = Element.begin(); it != Element.end(); ++it)
Elements.insert(pair<int, string>(it->second, it->first));
for(std::multimap<int, string>::reverse_iterator it = Elements.rbegin(); it != Elements.rend(); ++it) {
cout << "Element: " << it->second << " : " << it->first << endl;
}
This is how i did it . How to display only 10 elements and not all the elements.
View 17 Replies
View Related
Feb 24, 2015
How do I get c++ to read a file containing six numbers - a1, b1, a2, b2, a3, and b3 - that are constants for the following equations:
clubangle(degrees) = a1 + b1*0.85*clubnumber
clublength(inches) = a2 + b2*1.05*clubnumber
clubspeed(yards/s) = 1.1 * (a3 + b3 * swingnumber) * (clublength(inches)/40)^2;
View 5 Replies
View Related
Mar 29, 2014
I am doing this program where I analyze colors in a ppm file. Now I am having a hard time storing the data into arrays in this order...
P3
600 339
255
44 5 8
44 5 8
43 4 7
42 3 6
42 3 4
44 5 6
...
So far i tried using fscanf()
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
#define DEBUG true
#define CHECK true
[Code] ....
I then realized that the order that the second method gave me will make it very hard for me to calculate the RGBs. Because they will be calculated like wise..
P3
600 339
255
44 5 8 = sum
44 5 8 = sum
43 4 7 = sum
42 3 6 = sum
42 3 4 = sum
44 5 6 = sum
...
but that isn't the issue right now..
View 10 Replies
View Related
Oct 17, 2013
I have a file that can range from 100 rows to 10000+ that is comma delimited with 8 columns. The first 32 rows (also comma delimited) will always be ignored (geographical header information). I will be wanting the data from column2 and column3.
For this I believe I would need (2) For Loops as such:
for(i=0;i<2;++i) {
getline("do something here");
}
and
for (i=0;i<3;++i) {
getline("do something here")
}
Also would using a vector or array with dynamic storage be the better way to tackle this problem? Where to start from after accessing the file.
View 19 Replies
View Related
Mar 27, 2013
Is it possible to store data to CPU memory, if so how much memory can I store and how? Say I wanted to store an array, about 6MB in size?
View 1 Replies
View Related
Apr 12, 2013
I am trying to store the Title, Artist, and date published of a list of CD's. I can't seem to be able to get it to print the list or not sure if it is actually storing it. This is what i have so far.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
int main() {
char names[5][100];
[code]....
View 2 Replies
View Related
Jul 11, 2014
I am trying to create a generic map container which shell contain data of different datatypes. I already found a solution in this forum here:
[URL]...
Introducing a container with a Base Class as content type and inserting objectes of Derived Class types from that Base Class suites my implementation very well. But it is not really working. I implemented it this way:
class MyType {
public:
MyType() {}
virtual ~MyType() {}
}; template <class PT> class ParseType : public MyType
[Code]...
Then I insert one element.
// index being an object of type Parser<string>
ParseType<string>* test = new ParseType<string>( index );
// and index.val(0) = "-n"
iMap.insert( pair< string, MyType* >( index.id(0), test ) );
Now I think I should be able to call
const string key("-n");
iMap.at(key)->content->val(n);
Or
iMap.at(key)->get_val(n);
But neither one compiles with the error that "class MyType" (as the map container is pointing to a MyClass object) has no member/member function "content"/"get_val".
I also tried introducing member and member function "content" and "get_val" in "class MyType", which I thought should be hidden while constructing ParseType<string>. This would compile but it does not call the member "content or member function "get_val" from class ParseType.
A second try was to remove templatization of "class ParseType" and introduce a specific, let's say, "class ParseString" for objects of type Parser<string>. But the problems remain the same either the compiler complains due to missing member/member function or retreiving the map content will not call the derived class member/member function.
View 4 Replies
View Related
Feb 21, 2013
I am working on a small c++ project where i read from a csv file with information in this format:
string,string,int
string,string,int
string,string,int
And all of the above information is one person. As is "John,Peter,23" is first name, last name, age. When there are multiple people in a csv file how can i parse through and separate the information in a string and int for later use?
View 4 Replies
View Related
Jan 15, 2013
I have a set of functions at work which are incredibly useful, however it only supports labels that come from a specific database because that database contains information on the type. I'd like to re-create it to make it more applicable to any member/static/global variables but don't know how to store the type.
I've already re-written it to an extent, but it only accepts int types. I'd like to template it to accept any type. The trick is storing that type so that the pointer can be dereferenced at a later time which I don't know how to do.
Interface:
typedef int T; // The goal is to remove this line!
namespace TimerDelay {
void SetAfterDelay ( T* lpLabelAddress, float delay, T target = T(1)); // Queues the set
void ManageDelays ( float dt ); // sets the labels when appropriate
}
Source:
#include <vector>
namespace TimerDelay{
struct DelayObject {
void* address; // I will probably need to add a container
void* target; // to hold the type, but how can this be done?
[code]....
Edit:Is it possible to store a std::iterator_traits<> struct as a member of my structure? The g_list isn't templated as it needs to accept all types at the same time. That means that DelayObject cannot be templated. I think that means that I cannot use a templated member class as the size may be inconsistant.
View 2 Replies
View Related
Mar 27, 2013
Im tasked with reading a data file, this is an example snippet
list of trophy winners
year facup leaguecup 1stdiv 2ndiv
1960/61 Tottenham Hotspur Aston Villa Tottenham Hotspur Ipswich Town
1961/62 Tottenham Hotspur Norwich City Ipswich Town Liverpool
1962/63 Manchester Utd Birmingham City Everton Stoke City
The file starts in 1892 and is up to 2011/12, there is data missing for some years due to the wars etc,
once ive read the file, i need to store the data, for me to re-use.
There are a lot of useful link regarding reading data in, but they tend to be with very small files with say 10 lines of numbers.
View 1 Replies
View Related
Nov 28, 2012
I have to store large amount of data and retrieve the same data then write into file in C++. Currently I am using vector to store and retrieve. But vector is taking more time to store and retrieve the element. Is any other best data structure to store and retrieve large amount of data in unordered way?
Example code:
int I1 = 700,I2 = 32, I3 = 16;
//declare and resize the vector size
vector< vector < vector < vector<DOUBLE> > > > vPARAM;
vPARAM.resize(I1, vector< vector < vector<DOUBLE> > >
[Code] ....
View 3 Replies
View Related
Aug 5, 2014
I'm having trouble reading my data from a .txt file into a structure of the format shown in my code. I've made my student database in the program below based on user input and I didn't have a problem with that, but now it's come to input from a file it's making it difficult.
My three tasks are:
(1) A table containing 1 row per student, containing the student ID number and all of the student's marks.
(2) Another table, containing 1 row per student, containing the student ID number and the average mark obtained by that student.
(3) The average mark for each subject.
The assumptions to be made are: The student ID can begin with a zero, and should therefore be read in as a string.The test data will contain 20 students but the program should be able to deal with up to 100 students.Assume there are no more than 4 different subjects.
So based on the first assumption I've arranged the data in the file in an order in which the student ID begins with a zero:
003953 Computing 38
003953 English 88
003953 Mathematics 29
003953 Physics 83
073241 Computing 63
073241 English 99
073241 Mathematics 32
073241 Physics 73
...for twenty students, 80 lines of data. Now, on the assumption they must be read in as strings, this is what's making it tricky to store in the structure because, I've got 80 ID numbers but 20 repeat themselves 4 times. Once I've got this data in the structure below the tasks I won't have a problem with because I can just base it on a user input program but the data's already stored instead.
Below is my code for user input associated with task (1). In this example the IDs are stored as ints but for the file they will be strings. It compiles fine, displays the data as shown in the assignment sheet, but I don't know how to get the data into my structure. I can store the data in a structure of three arrays using fscanf() no problem, but it's not very "workable" for what I need to do with it.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
[Code].....
View 11 Replies
View Related
Dec 5, 2014
I am trying to read a file use the data line by line to create into an object. The current file I have is like this and the code reading the file will be found below.
1223 Fake1 Name1 60 70 80 24 89 add1 Male
1224 Fake2 Name2 61 70 81 80 24 add2 Male
1225 Fake3 Name3 63 70 82 80 89 add3 Male
1226 Fake4 Name4 63 70 83 80 88 add4 Male
The problem I am having is that I need to put delimiters in the file so that a person can have more than one name and also the address can now hold multiple strings until the delimiter.
I would like to change my file to this;
1223 : Fake1 Name1 : 60 : 70 : 80 : 24 :89 : This will be address1 : Male
1224 : Fake2 Name2 : 61 : 70 : 81 : 80 :24 : This will be address2 : Male
1225 : Fake3 Name3 : 63 : 70 : 82 : 80 :89 : This will be address3 : Male
1226 : Fake4 Name4 : 63 : 70 : 83 : 80 :88 : This will be address4 : Male
How can I update the code below so that it can use the delimiters to create an object?
void loadFile(Person people[], int* i) {
ifstream infile("people2.txt");
if ( !infile.is_open()) {
// The file could not be opened
cout << "Error";
[Code] .....
View 5 Replies
View Related
Mar 5, 2014
Following function is from a bank account, when new account is created it won't store it in the array or i guess it does but find_acct function wont be able to find it.
find_acct Function:
int findacct(int acctnum_array[], int num_accts, int requested_account) {
for (int index = 0; index < num_accts; index++)
if (acctnum_array[index] == requested_account)
return index;
return -1;
[Code] ....
View 2 Replies
View Related
Jan 6, 2014
I am reading data from a text file into a program. I am well aware of the subtle distinctions in the mode of data input/entry when using the stream extraction operator, the get() function, and the getline() function.
My problem is that all of them do not read and/or store the newline character alongside the data read!
Any function that reads and stores data and the terminating newline character together??
View 1 Replies
View Related
Feb 20, 2015
I have a structure
struct Wine
{
string wineName;
int vintage;
int rating;
double price;
};
how can i store the file data below in an array with the structure type???
Dow Vintage Port ;2011;99;82
Mollydooker Shiraz Carnival of Love ;2012;95;75
Prats & Symington Douro Chryseia ;2011;97;55
Quinta do Vale Meão Douro ;2011;97;76
Leeuwin Chardonnay River Art Series ;2011;96;89
View 1 Replies
View Related
Jun 27, 2014
I am doing SerialPort communication program where i am opening the port and reading data from port and storing in one document.
I debug my program and check that my read function is reading the data properly but when checking the document i find only unicode.
Suppose I am sending 1,2,3,4 to port i able to read same but my document showes þÿÿ þÿÿ þÿÿ þÿÿ
//My Read Function which read data from serial port
tInt serialport::Read(tVoid* pvBuffer, tInt nBufferSize) {
tInt nBytesRead = 0;
[Code] ....
Why I am getting unicode instead of 1,2,3,4 in my document ....
View 3 Replies
View Related
Feb 20, 2015
Can distinguish between character constant and string constant giving examples
View 1 Replies
View Related
Jul 21, 2014
How to tell whether a variable has been accessed in memory, no matter what it's been used for... Whether it's actually been set to something else, or whether it's been get for an operation or function call that doesn't actually do anything to affect the variable itself. Either way the variable has been "accessed" for something.
I'm not entirely certain that it's possible to detect a program getting the variable but I know that programs exist where they can trace what has accessed a certain part of memory... CheatEngine is one example, although I'm not entirely sure whether this can only detect changes in the variable and then trace what did it
View 4 Replies
View Related
May 17, 2013
Suppose I have a class "A", which has a method "void AMP_call()" that calls paralel_for_each in which another method, "float amp_function(float) restrict(amp)". When I call that method, can it then use members of "A"?
class A {
void AMP_call();
float amp_function(float) restrict(amp); // do something on a device
float allowed_variable;
std::vector<bool> not_allowed;
[Code] ....
Another way to frame my question, perhaps to make it easier to understand what I am after, would be that I want to know what happens if an amp-restricted method is called where the body of the class itself (which is not amp-compatible and afaik doesn't have to be since it's not passed to the device) may contain members that are not amp-compatible.
All of the msdn blogs I could find deal with which functions and methods can be called from within a parallel_for_each loop, but not with which variables are available to the lambda function itself.
View 9 Replies
View Related