РЕДАКТИРОВАНИЕ : пример Jon намного лучше, хотя я думаю, что дополнительный метод является все еще способом пойти так, Вы не должны повторять код везде. Я удалил замену и заменил первым примером Jon на месте в дополнительном методе. Мои приложения обычно являются приложениями интранет, и я не должен волноваться о неамериканских культурах.
Добавляют дополнительный метод, чтобы сделать это для Вас.
public static class DateTimeExtensions
{
public static string MyDateFormat( this DateTime dateTime )
{
return dateTime.ToString("dddd, MMMM d, yyyy a\\t h:mm") +
dateTime.ToString("tt").ToLower();
}
}
...
item.PostedOn.MyDateFormat();
РЕДАКТИРОВАНИЕ : Другие идеи о том, как сделать это в , Как отформатировать DateTime как " 10 октября 2008 10:43 CST" в C#.
Шаг 1: Не делайте этого:
while(!file.eof())
{
getline(file,line);
numlines++;
}
numline--;
EOF неверен, пока вы не попытаетесь прочесть его. Стандартный шаблон:
while(getline(file,line))
{
++numline;
}
Также обратите внимание, что std :: getline ()
может дополнительно принимать третий параметр. Это персонаж, на которого стоит остановиться. По умолчанию это признак конца строки, но вы можете указать запятую.
while(getline(file,line))
{
std::stringstream linestream(line);
std::string value;
while(getline(linestream,value,','))
{
std::cout << "Value(" << value << ")\n";
}
std::cout << "Line Finished" << std::endl;
}
Если вы храните все значения в одном векторе, то распечатайте их с фиксированной шириной. Тогда я бы сделал что-то вроде этого.
struct LineWriter
{
LineWriter(std::ostream& str,int size)
:m_str(str)
,m_size(size)
,m_current(0)
{}
// The std::copy() does assignement to an iterator.
// This looks like this (*result) = <value>;
// So overide the operator * and the operator = to
LineWriter& operator*() {return *this;}
void operator=(int val)
{
++m_current;
m_str << val << (((m_current % m_size) == 0)?"\n":",");
}
// std::copy() increments the iterator. But this is not usfull here
// so just implement too empty methods to handle the increment.
void operator++() {}
void operator++(int) {}
// Local data.
std::ostream& m_str;
int const m_size;
int m_current;
};
void printCommaSepFixedSizeLinesFromVector(std::vector const& data,int linesize)
{
std::copy(data.begin(),data.end(),LineWriter(std::cout,linesize));
}