20070628

C++: numerical to string convertion using Boost lexical_cast.

#include <string>
#include
"boost/lexical_cast.hpp"

using namespace std;
// class ~ typename = same thing
template <class T>
string to_string(const T& arg) {
try {
return boost::lexical_cast<string>(arg);
}
catch(boost::bad_lexical_cast& e) {
return "";
}
}
Παράδειγμα χρήσης:
float float_nr=31212.434;
string float_str=to_string(float_nr);
// float_str = "31212.43"

C++: string to numeric convertion using stringstream.

// Μετατροπή string σε αριθμούς με χρήση της istringstream, η 3η παράμετρος μπορεί
// να είναι std::hex (16αδικό), std::dec (δεκαδικό) ή std::oct (οκταδικό)

template <class T>
bool from_string(T& t, const string& s, ios_base& (*f)(ios_base&)=dec) {
// Πέρασε το input string s στο iss
istringstream iss(s);
// Γράψε το αποτέλεσμα στο t (template variable)
return !(iss >> f >> t).fail();
}
Παράδειγμα χρήσης:
string str="31212.43343535";
double d_nr;
from_string<double>(d_nr,str);
// d_nr = 31212.433435350002
int i_nr;
from_string<int>(i_nr,str);
// i_nr = 31212
float f_nr;
from_string<float>(f_nr,str);
// f_nr = 31212.434
str="ff";
int i;
from_string<int>(i,str,hex);
// i = 255 (η τιμή 255 είναι το FF σε 16αδικό)
Link: C++ String: How to convert a string into a numeric type?
Μπορούμε επίσης εναλλακτικά να χρησιμοποιήσουμε την Boost βιβλιοθήκη boost/lexical_cast.hpp:
#include "boost/lexical_cast.hpp"
using boost::lexical_cast;
using boost::bad_lexical_cast;

string str="31212.43343535";
d_nr=lexical_cast<double>(str);
// d_nr = 31212.433435350002

// Εδώ ρίχνει μια bad_lexical_cast exception!
try {
i_nr=lexical_cast<int>(str);
}
catch (bad_lexical_cast &) { }
// i_nr = ??
f_nr=lexical_cast<float>(str);
// f_nr = 31212.434
Link: Convert a string into a numeric type using Boost.

C++: at() function vs [] operator.

// αυτό αρχικοποιεί 5 τιμές του διανύσματος με τιμή 1
vector<int> v( 5, 1 );

// θα δώσει επικίνδυνα αποτελέσματα όταν i>=5
for( int i = 0; i < 10; i++ ) {
cout << "Element " << i << " is " << v[i] << endl;
}

// Αυτό είναι λιγότερο ασφαλές γιατί μέσω του at() γίνεται
// throw exception για i>=5
for( int i = 0; i < 10; i++ ) {
cout << "Element " << i << " is " << v.at(i) << endl;
}

20070618

Praat: Convert Sample points to time to zero-crossing.

Read from file... my_wav.wav
sound = selected("Sound")
# το 2323 είναι έστω το δείγμα μας
call zero_crossing 2323 sound
# Η μεταβλητή zerocrossing περιέχει το δείγμα σε μορφή zero-crossed
printline zerocrossing='zerocrossing'

# H συνάρτηση αυτή παίρνει ως είσοδο το sample και το
# αντικείμενο sound και με την μεταβλητή zerocrossing
# επιστρέφει τον χρόνο με το sample σε zerocrossing
procedure zero_crossing sample sound
select sound
# convert sample -> time
time=Get time from sample number... sample
printline time='time'
# do zero-crossing
left_zero_cross=Get nearest zero crossing... Left time
right_zero_cross=Get nearest zero crossing... Right time
# Θεωρητικά αυτός ο έλεγχος είναι υπερβολικός αλλά που ξέρεις καμιά φορά..
if (left_zero_cross<>right_zero_cross)
printline Different Left and Right zero crossing!
printline Left='left_zero_cross'
printline Right='right_zero_cross'
else
printline zero_crossing='left_zero_cross'
zerocrossing=left_zero_cross
endif
endproc

C++ : Συνάρτηση για έλεγχο ύπαρξης ενός αρχείου στο δίσκο.

bool fileExists(const std::string& fileName) {
std::fstream fin;
fin.open(fileName.c_str(),std::ios::in);
if (fin.is_open()) {
fin.close();
return true;
}
fin.close();
return false;
}

eggs.in.art (my non-technical blog)