string to int


See On Github

Data

Contributor

Generic placeholder thumbnail

by ivanchoff

in c++

Tags

conversion

Source Code

CC= g++ -std=c++11

all: string_to_int

string_to_int: string_to_int.cc
	$(CC) string_to_int.cc string_to_int_test.cc -o string_to_int.out

clean:
	rm -rf *.out
#include <sstream>
#include <string>

/*
 * Return int from c++ string
 */

int string_to_int(std::string x){
    std::stringstream s;
    s << x;
    int r;
    s >> r;
    return r;
}
#ifndef str_to_int
#include <string>
#define str_to_int

/*
 * convert c++ string to int
 */

int string_to_int(std::string x);

#endif
#include<iostream>
#include<cassert>
#include "string_to_int.hpp"

using namespace std;

/*
 *    Testing with 10 strings
 */

int main(){
    //string array for test
    std::string str[10] = {"12","99","123","768","892","445123","12389","123456","9871","987654321"};
    int sol[10] = {12,99,123,768,892,445123,12389,123456,9871,987654321};

    for(int i=0; i<10; ++i){
        //check returned value from string_to_int function
        assert(string_to_int(str[i]) == sol[i]);
    }
    cout<<"Tested passed!"<<endl;
    return 0;

}