C++ :: Fastest Way Of Passing Data To A Class

Apr 25, 2013

I'm currently building a new data structures that will be used in monte carlo generators (and so will be constructed several million times) and I'm wondering what's the best way (computer-speed-wise) to pass the data to the constructor. I am currently doing it using references and passing arrays as pointers like this:

Code:
class particle{
public:
particle(double *ar,int &id):IDup(id){
for (int i=0;i<5;++i)
Pup[i]=ar[i];
}
int IDup;
double Pup[5];
};

I'm assuming that since using references has no need to create a temporary memory slot it's more efficient .....

As for the arrays: is there a way for me to pass them as reference as well? (not using c++11), I'm using arrays instead of vectors as much as I can because I assume that vectors, being more advanced data structures, would take more time to create.

View 14 Replies


ADVERTISEMENT

C++ :: Passing Vector Of Class To Function Of Another Class?

Dec 14, 2014

im passing a vector of a class to a function of another class. But i cant access the data on the classes inside the vector.

Something like that:

class CDummy{
...
public:
string m_name;

[Code].....

Im creating the vector on main() and using push_back with a pointer to an initialized CDummy instance

View 5 Replies View Related

C++ :: Passing Class As Type In Template Class

Nov 30, 2013

I am trying to pass a class as a type to a template class. This class's constructor needs an argument but I cannot find the correct syntax. Is it possible?

Here is an example of what I described above. I did not compiled it, it is for illustrative purpose only. And of course argument val of the myData constructor would be doing something more useful than simply initializing an int....

template <class T>
class templateClass {
templateClass() {};

[Code]....

My real code would only compile is I add the myData constructor:

myData () {};

and gdb confirmed that it is this constructor that get called, even with dummy(4).

View 4 Replies View Related

C++ :: Fastest Way To Calculate Fibonacci

Apr 1, 2013

What is the most efficient way to calculate Fibonacci. Is it recursion with memorization, is it iteration or is there another approach?

View 1 Replies View Related

C/C++ :: Fastest / Most Efficient Way To Add Two Fractions

Jul 12, 2012

I have to write a function

struct rNumber add(rNumber a ,rNumber b); 

which adds two rational numbers in following representation :
rNumber := s*(n/d)* 2^e
struct rNumber{
 _byte_t s; // sign (do not consider for this question)
 uint n; //numerator
 uint d;// denominator
 short e;//exponent
}  

If the exponents of both numbers are not equal, then they have to be made equal in order to add them. This can be made in 4 ways : increase or decrease the n or d of both numbers.

But if we decrease the denominator of a number (a.d =1) by shifting it for example 1 bit to the right, we get 0 which leads to INFINITY for the fraction. In another case decreasing the numerator would lead the n to be 0 which meanse the whole fraction is then 0.

According to this, in worst case, all 4 cases has to be checked for the right result.

So far the UNDERFLOW of n or d is considered. If we try to increase the value of n or d, then OVERFLOW may also occur.

The very first, intuitive solution would be iteratively increase/decrease one of the terms and to check if the change leads to ZERO or INFINITY.

View 5 Replies View Related

C :: Fastest Way To Check If Bytes Contained In Array Are All X00

Nov 30, 2014

I'll process the raw content of a stream which obviously will be loaded one chunk at a time into an buffer.I need to check if every chunk content is x00 filled. If it is, I'll increase the blank chunks counter of 1.On your personal opinion, which is the fastest an less cycles consuming manner to achieve such result?

I was wondering this: is it possible to make an instant XOR of all the buffer content to check if it does return 0 ?the only way is it to cycle through all the bytes and compare each one of them with 0?

View 1 Replies View Related

C++ :: Absolute Fastest Container To Lookup Names

Jan 9, 2013

Names are std::string no more than 3 characters in length. What built in container or boost container is fastest at finding a name (Key) in it's container.

Finding a name.
At the name will be a value double.

I need the most efficient at finding and finding/updating the value. Forget out inserts and deletes.

View 2 Replies View Related

C :: Fastest Way Of Changing Elements In Array Of Characters For Encode

Feb 15, 2014

I am wonder what is the fastest way of changing elements in array of characters for encode purpose, here is simple example presenting an idea:

Code:

char *a = "123456789":
char b[] = "ABCDEFGHI";
int 1;
for (i=0;i<strlen(a);i++) {

b[i] = a[i];
}

Is this approach better or worse than bit manipulation ?

View 4 Replies View Related

C++ :: Fastest Comparison Algorithm Based On A Predefined Container?

Sep 3, 2014

Suppose you have defined a container of elements and you want do define a comparison function between elements based on the ordering of the elements in that container. What algorithm for the comparison would be the most efficient?

Example:
master = {tom, sam, mary, sally, bert, frank}
subset = {bert, sam, mary, tom}
goal: change subset to {tom, sam, mary, bert}

My current idea is to simply iterate from the beginning of the container, and whichever of the two elements is found first is the lesser (assuming the second is not the same as the first). It seems kind of naïve though. Any better performing algorithm? This is what I have so far:

#include <iostream>
#include <iterator>
#include <list>
#include <vector>
#include <stdexcept>
#include <algorithm>

[code]....

Would perhaps forcing the container to have a random access iterator, like vector, and then writing a specialized comparison function based on that perform even faster? Or perhaps force the container to be a map to integers, and compare the elements by comparing the integer mapped values?

View 19 Replies View Related

C :: Passing Data From An Array And Using It In Another

Mar 18, 2014

I have been asked to write a lottery program in c using 5 separate functions 1 to take 6 numbers from user 2 to display the numbers 3 to sort the numbers 4 compare the chosen numbers to the winning numbers and 5 to list the frequency the user pick a number depending on how many times they play the game the problem im having is with the first 2 functions i can take the input and pass it to a display function but cannot get it to display the numbers here is what i have. The output from this is at the bottom .....

Code:
//function prototypes
int myNumbers();
void displayNums();
void sort();
void compare();

#include <stdio.h>
#define NUMS 6
#define WIN 7

[Code] .....

"Please enter your lucky numbers
1
2
3
4
5
6
Your numbers are 2286612"

View 1 Replies View Related

C# :: DLL Passing And Retrieving Data

Sep 18, 2013

I have a C# program going and want to be able to call the DLL and receive back the data requested through a pointer.Below is the DLL import within my C# code.

Code:

[DllImport("MyTest.dll")]
public static extern int ReadNetwork(Byte[] ROM_ID); Below is the code in the DLL Code: int _stdcall ReadNetwork(unsigned char* Array1)
{
ReadDevice(readBackArray);
for(i = 0; i < 20; i++)
{
Array1[i] = readBackArray[i];
}

[code]....

I've tried changing the return values in the DLL's ReadNetwork() function and that works ok, so I know I'm calling the DLL and it runs ok, but printing the result back is where I'm having the problem.

View 2 Replies View Related

C :: Passing Arrays To Other Data Has Pointers?

May 7, 2013

I am looking for an example of when passing arrays to other data that has pointers,

I understand the terms I would just like to see a small example of code that actually demonstrates this process.

View 4 Replies View Related

C++ :: Passing Data Through A Void Function

Mar 11, 2013

I am new to C++ ... My question is if I were to pass some data or values from the main() function through a void function (which main purpose is to display the data in a certain manner); how should one go about doing so?

For instance:

Code:
// example.cpp -- poorly written, just trying to learn
#include <iostream>
void format() {
using namespace std;
cout << "Time: " << min << hrs << endl;

[Code] .....

Obviously this is really poorly written, and confusing for the user. My main goal is to learn how to pass through values to a void function.

Also a bit off-topic, but is there a downside to place "using namespace std;" outside a function say if out of 100 functions only 10 of them use it? Would it make the program slower/unstable in any way or is this something one could do without any downsides?

View 3 Replies View Related

C# :: Passing Additional Data To PropertyGrid / TypeConverter

May 29, 2014

The problem is that my DataSelector form, which is instigated from the PropertyGrid (by creating a UITypeEditor) has access to the property LinkedDataItem. However I want the DataSelector form, which currently lists all the DataItems in the DataManager, to only list DataItems which are supported by the CustomControl, therefore my DataSelector class also need the "SupportedTypes" property from the Custom Control. How can I do this?

I thought maybe I can use a type converter to reference the custom control itself, instead of just the LinkedDataItem, and just show it as the Linked DataItem, but I am unsure if this is the correct use or how to do this.

View 12 Replies View Related

C++ :: Passing Data Between Threads Without Using Global Variables

Nov 6, 2013

I have a main thread and a worker thread. How do i pass data between them when both are already running without using global variables?

View 1 Replies View Related

C++ :: Passing Data Structures To Function Pointers And Get Values

Sep 26, 2014

typedef struct example_dt_struct {
int a;
float b;
char* c;
};
typedef void(*func)(example_dt_struct *s, int e, int f);
void f(func *n){}

how can i use example_dt_structure with my function pointer into f()

View 5 Replies View Related

C++ ::  Passing Class Instance To Function

Jan 17, 2014

I know that it is possible to pass a class instance to a function, but in my experience, if said function changes any variables of the class, they don't actually get changed. For example, we have class object, that has a member int number = 5. Lets say we have two functions, func1() and func2, which are not members of class object. If we pass object to func1() which, lets say, increases number by 5 (so now number = 10), at the end of that function number still = 5.

Is there a way to bypass this and have functions alter class variables permanently?

I know that I can pass variables by reference, but, in my experience, such a thing does not work with vectors (which I am also dealing with), so simple passing the desired variables by reference won't work.

View 5 Replies View Related

C++ :: Possible To Overload A Class For Passing To A Function

Feb 1, 2014

I want to be able to do

someFunction(MyObject)

rather than

someFunction(MyObject.getWhatIWant())

I know I can do &MyObject, MyObject() and MyObject[0] by overloading operators but is it possible with just plain old MyObject?

this is assuming that I have no access to someFunction(), i.e, it cannot be modified.

View 7 Replies View Related

C++ :: Passing Object To Inherited Class By Constructor

May 1, 2013

I am trying to pass an object to an inherited clas by a constructor. I am having Cboard my board and i need to pass it to the object Cpawn.

Here under is my code:

main
Code:
#include<iostream>#include "Cboard.h"
#include "Cpawn.h"
#include "Cpiece.h"

void main(){
char location[50];

[Code] ....

View 3 Replies View Related

C++ :: Passing Char Arrays Between Functions Of Same Class

Nov 16, 2013

I'm having trouble with passing a character array between functions of the same class. I have a function, buildGraph, that calls function getNextLine. The getNextLine essentially just retrieves the next line of an input file and stores it into a "char line[80]". However when I try to use "line" in my buildGraph function, it has nothing in it.

Here's my code:

Class
#define NUMNODES 10
using namespace std;
#pragma once
class Prog3Graph

[Code] ....

View 4 Replies View Related

Visual C++ :: Passing Class Pointer Via Socket

Oct 14, 2013

I have a function called,

App 1:
void GetImage(CImage * img) {
//Pass &img over socket to another app
}

App 2:
void DisplayImage() {
CImage * pImg = &img;
}

Is it possible to pass a class pointer as memory buffer across the socket? The above code is just an example. My question in general is, whether it's possible to pass any Classes pointer as a memory buffer across sockets.

View 7 Replies View Related

C/C++ :: Passing Var Assigned In A Class File To Functions Outside Of Int Main?

Dec 7, 2014

I'm trying to pass the value of an object created from a class file to a function outside of the "Int Main" function in the main.cpp file. I've successfully created the object, I just want to pass it to a void function but I'm getting the scope error below. I'm not sure how to correct. I'm not having much luck with research either (static variables?).

error: 'TicTacToe' was not declared in this scope

main.cpp
#include <iostream>
#include <cstdlib>
#include "Signature.h"
#include "Gameplay.h"
using namespace std;
// Initialize array
char square[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

[code]....

View 4 Replies View Related

C++ :: Passing A Function Pointer As Template Argument To A Class

Aug 15, 2012

I have in the past written code for templated functions where one function argument can be either a function pointer or a Functor. Works pretty straightforward.

Now I am in a situation where I am actually trying to pass a function pointer as template argument to a class. Unfortunately this does not work, I can pass the Functor class but not the function pointer. Below code illustrates the issue:

Code:
#include <string>
#include <iostream>
#include <sstream>
#include <cstdlib>
// For demonstration
const char * external_library_call() {
return "FFFF";

[Code] .....

The idea is to have the definition of the Record class simple and readable and have a maintainable way to add auto-conversion functions to the class. So the lines I commented out are the desirable way how I want my code to look. Unfortunately I could not come up with any way that was close to readable for solving this.

View 3 Replies View Related

C++ :: Unordered Map With Class Data

Aug 27, 2013

I'm trying to make an unordered_map with a key of std::string and a data type of my own class, "Variable". This is my code that I've done:

#include <iostream>
#include <sstream>
#include <string>
#include <unordered_map>
class Variable { // The class that I want to have as data type
public:
Variable(double value) : _value(value) {}

[Code] .....

This code works just fine when I use double instead of "Variable", but the moment I put in the class "Variable" instead it gives my this massive error:

(cut for clarity)main.cpp|25|error: conversion from 'std::_Hashtable<std::basic_string<char>, std::pair<const std::basic_string<char>, Variable>, std::allocator<std::pair<const std::basic_string<char>, Variable> >, std::_Select1st<std::pair<const std::basic_string<char>, Variable> >, std::equal_to<std::basic_string<char> >, std::hash<std::basic_string<char> >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, true, false, true>::_Insert_Return_Type {aka std::pair<std::__detail|

View 4 Replies View Related

C/C++ :: Data In Private Class

Dec 11, 2014

i have private data look like Peiceorder peiceOrders[20]; it looks like a array but does not have name type like int or char? why is that and what should I use the data should I write Peiceorder peiceOrders[20]=........ like that? or?

View 1 Replies View Related

C++ :: Static Data Of A Class

Oct 14, 2012

I am studying the use of static data into classes and the code below is returning an error ...

#include <iostream>
using namespace std;
class REC {
private:
static int n; //Dado que sera unico na classe e alterado por todos os objetos

[Code] .....

The error:
main.obj:-1: error: LNK2001: unresolved external symbol "private: static int REC::n" (?n@REC@@0HA)
Note: I'm using QT Creator

View 3 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved