Dynamic variable names by concatenating integer.

Hi

Dear i want to concatenate integer (1......n) with char like A or B to generate variable names dynamically.

like
v1
v2
v3

Please help me how i can do this .
Thanks in advance.

Last edited on
One possibility are stringstreams:
1
2
3
stringstream ss;
ss << 'v' << someNumber;
string name=ss.str();


Another way is to use boost.lexical_cast or something comparable:
string name="v"+lexical_cast<string>(someNumber);
I'm assuming the OP wants to generate symbol names dynamically; with v1, v2, etc. being ints or something. If that's the case, you can't do that in C++; the best alternative I think would be using a map of std::string to your data type and then generate the names as Athar suggested.
Why do you need to do this?
1
2
3
4
5
6
7
8
9
10
11
#include <map>
#include <string>
using namespace std;

int main()
{
	map<string, int> Vars;

	Vars["MyVarName1"] = 7;
	Vars["MyVarName2"] = 42;
}
+1 Galik

This smells of someone asking how to do the wrong thing.

Maybe a vector will work? Instead of v1 = 3; v2 = 4; you can use a vector/array and have v[0] = 3; v[1] = 4;.

It's very simple:

1
2
3
4
5
6
std::vector<int> v( n );  // where 'n' is the number of variables you want

v[0] = 1;
v[1] = 2;
...
v[n-1] = n;  // the last element in the array 
Aren't vectors auto expanding for assignment? In that case you shouldn't need to specify the size, right?
Last edited on
Aren't vectors auto expanding for assignment?

No...
Huh, sorry, my mistake. Someone has lied to me then >_>
Well, they expand automatically if you use push_back.
But not when using operator=.
Thank you for this discussion, actually i needs to generate dynamic variables names to store chi square statistical test results temporarily.

chi2 test take lot of time in calculations , i want to store chi2 test values as a cashe in my program and each time i want to search whether it is already calculated or not.

Test formate is like this:
inputs:
(a,b) given (c, d, ........(not fixed, may be zero or one or more than one))

outputs:
bool test, double chi2_value, double P_value.


so, there are two main problems
1- how to store both in put and output values?
2- how i can implement search on input values? (input variables can change their orders so, search can be done by any order like ab, ba, or v5v3 or v3v5)

so, for this purpose i declare a user defined datatype by using "Struct"
then i create a static vector of mydatatype in header file.

variables "a" and "b" are integer type which contains values between 1...8;

Now i want to combine these two variables to generate new name then to store in vectore or map.

My last question from the experts, whether this way of cashing the values during the program runtime is good or not ? Or is there any more better way?

Again thanks for your contribution in this discussion


I don't completely understand what you are trying to do, but is it possible for you to store your variables in a 2D matrix for each combination of a and b?
1
2
3
4
5
6
7
8
9
double cache[8][8]; 

cache[0][0]; // same as v1v1 ?
cache[0][1]; // same as v1v2 ?
cache[0][2]; // same as v1v3 ?

cache[1][0]; // same as v2v1 ?
cache[1][1]; // same as v2v2 ?
cache[1][2]; // same as v2v3 ? 

Does that help?
No, it can not work, because 1, 2, 3 are not continues sequence it is a random number between 1 and 8.

chi2 is a probability test between two variables given some other variables like

chi( a b | c d ...........)

Sounds like what you want is a map.
1
2
3
4
5
6
7
std::map<double,double> map;
for (double a=0;a<10.0;a++)
    map[a]=pow(a,a);

if (map.find(5.0)==map.end())
    map[5.0]=pow(5.0,5.0);
std::cout <<map[5.0]<<std::endl;
http://math.hws.edu/javamath/ryan/ChiSquare.html

it sounds like you are using Chi2 which is a test to see how the distributions of variables differ in a set of data. (look at link above)
It looks like you do not need dynamic variable naming at all.
You could use an array or a vector to remember input values, and just index them. or if you are expecting to have multiple SETs of inputs (corresponding to columns and rows in a table) then try using a map of vectors to remember the input data or a multidimensional array.

What you really need to do is write out exactly what you are trying to calculate, then work out an algorithm to do the calculation. That means you might have to do some algebra before you get to programming. At least, that is what i would do.
:)
as for bool test, double chi2_value, double P_value
have the function return test
have chi2_value and p_value be passed to the function as pointers to doubles, or just make them global variables.
Using a double/float as the key for a map seems like a bad idea. It would have all sorts of problems due to floating point error.
IMHO, Galik's answer is the closest to what you are trying to accomplish.
Maybe something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class OneSetOfInputs
{
  private:
    double m_inputs[100];  // or whatever you want your sample size to be
};

class OneChiSquareTest
{
  public:
    // arguments need to be between 0 and 7 - I leave it up to you to restrict their ranges
    void calculate( unsigned set1Idx, unsigned set2Idx )
    {
       if ( !alreadyCalculated ) {
         const OneSetOfInputs& set1 = myInputs[ set1Idx ];
         const OneSetOfInputs& set2 = myInputs[ set2Idx ];         
         // do work here ----------------------------------------------
         // work
         // work
         alreadyCalculated = true;
       }
    }
  private:
    bool alreadyCalculated;  // true if we can just use the results  
    bool test, double chi2_value, double P_value;
};

OneSetOfInputs myInputs[8];
OneChiSquareTest myCache[8][8];


With respect to whether you need to cache or not, you should ask yourself:
1. Q: Is the calculation expensive?
A: It could be, in this case. From a theoretical POV, your big O is not small. From a practical standpoint, if your sample size is big and if you have many sets, it could take some time. Best thing is to try some runs before caching.

2. Q: What is the chance you will hit the cache?
A: The probability, could, in fact, be very low. It depends on how you intend to randomize your input sample sets. If the chance of a hit is too low, it's not worth caching. If you intend to use a map, you need to think along the lines of Disch:
Using a double/float as the key for a map seems like a bad idea. It would have all sorts of problems due to floating point error.

In fact, your chance of getting a hit on such a cache is practically nil, if it weren't for pseudo-random number generators that depend on non-random seed values. If you don't like the 8x8 and want to do a larger set of inputs, you could do something like this:
1
2
3
4
5
6
class InputIdxPair {
  private:
    unsigned idx1, idx2;  // 0 to 499 in this case
};
OneSetOfInputs myInputs[500];
map<InputIdxPair,OneChiSquareTest> myCache;

But if you choose to use a large map (maybe sparse), you need to think carefully about your hit probabilities. If you only have 5 or 10 sets of inputs, then you may go for an array. If nSetsOfInputs gets large, maybe you should consider a map.

Most importantly, pay heed to backprop's comments. You need to concern yourself with the Chi squared algorithm first. Implement and test your basic algorithm. What inputs are you trying to randomize? In other words, define your sample sets very carefully.

If you still find it to be too slow, then think about caching.
Last edited on
Topic archived. No new replies allowed.