Alternative to std::stod

Hi,

After painstakingly coding this up I have been informed that std::stod from the <string> library won't work for this assignment as the default compiler at my school does not support c++11 ISO standard. What are my alternatives in this case?

I needed it to read things like this:

center_1[i-1] += stod(train_1.data[j][i])

I've provided the full code for context. Let me know if anything else will help. Thank you.

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "Matrix615.h"
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <string>

using namespace std;

double simpleClassifier(const char* train_dat_filename, const char* test_dat_filename)
{
    Matrix615<string> train_dat;
    Matrix615<string> test_dat;

    train_dat.readFromFile(train_dat_filename);
    test_dat.readFromFile(test_dat_filename);

    Matrix615<string> train_0;
    Matrix615<string> train_1;

    for(int i = 1; i < train_dat.colNums(); i++)
    {
    	if (train_dat.data[i][0].compare("1") == 0)
    	{
    		train_1.data.push_back(train_dat.data[i]);
    	}
    	else
    	{
    		train_0.data.push_back(train_dat.data[i]);
    	}
    }
  	
	vector<double> center_1(train_1.colNums()-1);
	vector<double> center_0(train_0.colNums()-1);
    
	for(int i=1; i < train_1.colNums(); i++)
	{
		for(int j=0; j < train_1.rowNums(); j++)
		{
			center_1[i-1] += stod(train_1.data[j][i]);
		}
		center_1[i-1] = center_1[i-1]/train_1.rowNums();
	}

	for(int i = 1; i < train_0.colNums(); i++)
	{
		for(int j = 0; j < train_0.rowNums(); j++)
		{
			center_0[i-1] += stod(train_0.data[j][i]);
		}
		center_0[i-1] = center_0[i-1]/train_0.rowNums();
	}

	vector<double> s_1(center_1.size());
	vector<double> s_0(center_0.size());

	for(int i=1; i < train_1.colNums(); i++)
	{
		for(int j=0; j < train_1.rowNums(); j++)
		{
			double average = center_1[i - 1];
			double reading = stod(train_1.data[j][i]);
			double tosquare = reading - average;
			s_1[i-1] += (tosquare*tosquare);
		}
		s_1[i-1] = s_1[i-1]/(train_1.rowNums()-1);
	}

	for(int i=1; i < train_0.colNums(); i++)
	{
		for(int j=0; j < train_0.rowNums(); j++)
		{
			double average = center_0[i - 1];
			double reading = stod(train_0.data[j][i]);
			double tosquare = reading - average;
			s_0[i-1] += (tosquare*tosquare);
		}
		s_0[i-1] = s_0[i-1]/(train_0.rowNums()-1);
	}

	vector <vector <double> > stretchedc_1;
	for(int i=0;i<train_dat.rowNums();i++)
	{
		stretchedc_1.push_back(center_1);
	}

	vector <vector <double> > stretchedc_0;
	for(int i = 0; i < train_dat.rowNums(); i++)
	{
		stretchedc_0.push_back(center_0);
	}

	vector <vector <double> > stretcheds_1;
	for(int i = 0; i < train_dat.rowNums(); i++)
	{
		stretcheds_1.push_back(s_1);
	}

	vector <vector <double> > stretcheds_0;
	for(int i = 0; i < train_dat.rowNums(); i++)
	{
		stretcheds_0.push_back(s_0);
	}

	vector < vector<double> > test_minus_stretch1;
	vector <double> nextRow1(test_dat.colNums()-1);
	vector <double> rowSum1(test_dat.rowNums()-1);

	for(int i= 1; i < test_dat.rowNums(); i++)
	{
		for(int j = 1; j < test_dat.colNums(); j++) 
		{

			nextRow1[j-1] = stod(test_dat.data[i][j]) - stretchedc_1[i-1][j-1];
			nextRow1[j-1] *= nextRow1[j-1];
			nextRow1[j-1] /= stretcheds_1[i-1][j-1];
			rowSum1[i-1] += nextRow1[j-1];
		}
		test_minus_stretch1.push_back(nextRow1);
	}

	vector < vector<double> > test_minus_stretch0;
	vector <double> nextRow0(test_dat.colNums()-1);
	vector <double> rowSum0(test_dat.rowNums()-1);

	for(int i= 1; i < test_dat.rowNums(); i++)
	{
		for(int j = 1; j < test_dat.colNums(); j++) 
		{

			nextRow0[j-1] = stod(test_dat.data[i][j]) - stretchedc_0[i-1][j-1];
			nextRow0[j-1] *= nextRow0[j-1];
			nextRow0[j-1] /= stretcheds_0[i-1][j-1];
			rowSum0[i-1] += nextRow0[j-1];
		}
		test_minus_stretch0.push_back(nextRow0);

	}

	vector <int> y_pred(rowSum1.size());
	for(int i = 0; i < rowSum1.size(); i++)
	{
		if (rowSum1[i] <= rowSum0[i])
		{
			y_pred[i] = 1;
		}
		else y_pred[i] = 0;
	}	

	vector <double> result(rowSum0.size());
	double pred = 0;
	for(int i=0;i<rowSum0.size();i++)
	{
		result[i] = (y_pred[i] == stoi(test_dat.data[i+1][0]));
		pred += result[i]/rowSum0.size();
	}
	cout << setprecision(4) << pred << endl;

	return 0.0;
}

int main(int argc, char** argv)
{
    simpleClassifier(argv[1], argv[2]);
    return 0;
}
What are my alternatives in this case?

I'd suggest stringstreams. The stringstreams are available in every standard version of C++.

Either use a stringstream (#include <sstream>) or strtod() (#include <cstdlib>).
Thank you both. I was able to use boost::lexical_cast, would stringstream or strtod be anymore efficient?
Since you can't use C++11 are you allowed to use boost?

I'd recommend the stringstream, since it is standard. You could even make your own function named stod() that use a stringstream internally.


Last edited on
The stod() function actually uses strtod() internally.
Strangely enough we are able to use the boost library. I haven't looked into stod boost alternatives.
Topic archived. No new replies allowed.