English 中文(简体)
将 std:: string 转换为 lip:: posix_ time::: ptime
原标题:Converting std::string to boost::posix_time::ptime
  • 时间:2012-05-24 06:50:00
  •  标签:
  • c++
  • boost

以下代码将 std::string 改为 boost:::posix_time::ptime

分析分析后,我发现大部分用于该函数的时间(大约90%)都浪费在分配 < code> time_ input_facet 的内存上。 我必须承认,我并不完全理解以下代码,特别是为什么必须将 < code> time_ input_facet 分配在自由内存上。

using boost::posix_time;

const ptime StringToPtime(const string &zeitstempel, const string &formatstring)
{
    stringstream ss;
    time_input_facet* input_facet = new time_input_facet();
    ss.imbue(locale(ss.getloc(), input_facet));
    input_facet->format(formatstring.c_str());

    ss.str(zeitstempel);
    ptime timestamp;

    ss >> timestamp;
    return timestamp;
}

" 强势 " 你找到任何办法摆脱分配吗? "/强势 "

最佳回答

使函数中的输入- 面静态:

static time_input_facet *input_facet = new time_input_facet();

这将仅在第一个函数调用时构造面部, 并重新使用面部。 我相信, 面部允许对同一对象进行多次调用 。

更新 : 您也不需要同时构建字符串流和本地化 。 只要在单独的函数中或者在静态初始化中做一次, 然后使用流 。

更新2:

const ptime StringToPtime(const string &zeitstempel, const string &formatstring)
{
  static stringstream ss;
  static time_input_facet *input_facet = NULL;
  if (!input_facet)
  {
    input_facet = new time_input_facet(1);
    ss.imbue(locale(locale(), input_facet));
  }

  input_facet->format(formatstring.c_str());

  ss.str(zeitstempel);
  ptime timestamp;

  ss >> timestamp;
  ss.clear();
  return timestamp;
}
问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签