Иногда массивы действительно лучше, чем векторы. Если Вы всегда управляете набором фиксированной длины объектов, массивы лучше. Рассмотрите следующие фрагменты кода:
int main() {
int v[3];
v[0]=1; v[1]=2;v[2]=3;
int sum;
int starttime=time(NULL);
cout << starttime << endl;
for (int i=0;i<50000;i++)
for (int j=0;j<10000;j++) {
X x(v);
sum+=x.first();
}
int endtime=time(NULL);
cout << endtime << endl;
cout << endtime - starttime << endl;
}
то, где векторная версия X
class X {
vector<int> vec;
public:
X(const vector<int>& v) {vec = v;}
int first() { return vec[0];}
};
и версия массива X:
class X {
int f[3];
public:
X(int a[]) {f[0]=a[0]; f[1]=a[1];f[2]=a[2];}
int first() { return f[0];}
};
желание версии массива основных () будет быстрее, потому что мы избегаем издержек "новых" каждый раз во внутреннем цикле.
(Этот код был отправлен на comp.lang.c ++ мной).
Используйте DateTime.TryParse вместо использования ваших собственных блоков try / catch.
string text = "10/16/2009";
DateTime result;
if (DateTime.TryParse(text, out result))
{
// success, result holds converted value
}
else
{
// failed
}
Лучшим шаблоном для синтаксического анализа даты и времени будет этот
string DateFormat = "dd/MM/yyyy"; //Or any other format
DateTime dateTime;
bool success = DateTime.TryParseExact(value, DateFormat,
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out dateTime);
Никогда не используйте DateTime.Parse и даже DateTime.TryParse
Попробуйте следующее:
DateTime todate;
if(!DateTime.TryParse("2009/31/01", todate))
{
//------------conversion failed-------------//
}
If you know what the format of the datetime will be, you can also use DateTime..::.TryParseExact Method
The DateTime.TryParse can cause problems when it is used with dates such as 01/03/2009
Is it 01 Mar or 03 Jan?
I would rather recomend that you use something other than a textbox, like a date picker, or validate the textbox so that you have something like dd MMM yyyy. very seldomly would you go wrong with that.
Little addition to previous answers:
DateTime:
public static DateTime Parse(string s)
{
return DateTimeParse.Parse(s, DateTimeFormatInfo.CurrentInfo, DateTimeStyles.None);
}
public static bool TryParse(string s, out DateTime result)
{
return DateTimeParse.TryParse(s, DateTimeFormatInfo.CurrentInfo, DateTimeStyles.None, out result);
}
DateTimeParse:
internal static DateTime Parse(string s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
{
DateTimeResult result = new DateTimeResult();
result.Init();
if (!TryParse(s, dtfi, styles, ref result))
{
throw GetDateTimeParseException(ref result);
}
return result.parsedDate;
}
TryParse is better
References: