0

変換方法

std::string strdate = "2012-06-25 05:32:06.963";

こんなものまで

std::string strintdate = "20120625053206963" // 基本的に -、:、スペース、.

strtok または文字列関数を使用する必要があると思いますが、それを行うことができません。ここでサンプル コードを教えてください。

を使用して unsigned __int64 に変換するように

// crt_strtoui64.c
#include <stdio.h>

unsigned __int64 atoui64(const char *szUnsignedInt) {
   return _strtoui64(szUnsignedInt, NULL, 10);
}

int main() {
   unsigned __int64 u = atoui64("18446744073709551615");
   printf( "u = %I64u\n", u );
}
4

6 に答える 6

3
bool nondigit(char c) {
    return c < '0' || c > '9';
}

std::string strdate = "2012-06-25 05:32:06.963";
strdate.erase(
    std::remove_if(strdate.begin(), strdate.end(), nondigit),
    strdate.end()
);

std::istringstream ss(strdate);
unsigned __int64 result;
if (ss >> result) {
    // success
} else {
    // handle failure
}

ところで、64 ビット int としての表現は少し壊れやすいかもしれません。2012-06-25 05:32:06日付/時刻が として入力されていることを確認してください2012-06-25 05:32:06.000。そうしないと、末尾から得られる整数が予想よりも小さくなります (したがって、西暦 2 年の日付/時刻と混同される可能性があります)。

于 2012-06-26T10:05:15.970 に答える
2

コンパイラが C++11 機能をサポートしている場合:

#include <iostream>
#include <algorithm>
#include <string>

int main()
{
    std::string s("2012-06-25 05:32:06.963");
    s.erase(std::remove_if(s.begin(),
                           s.end(),
                           [](const char a_c) { return !isdigit(a_c); }),
            s.end());
    std::cout << s << "\n";
    return 0;
}
于 2012-06-26T10:11:12.543 に答える
0

文字列置換を使用して、不要な文字を文字なしに置き換え ます http://www.cplusplus.com/reference/string/string/replace/

于 2012-06-26T10:02:47.287 に答える
0

どうぞ:

bool not_digit (int c) { return !std::isdigit(c); }

std::string date="2012-06-25 05:32:06.963";
// construct a new string
std::string intdate(date.begin(), std::remove_if(date.begin(), date.end(), not_digit));
于 2012-06-26T10:06:18.597 に答える
0

私はstrtokを使用しません。std::stringメンバー関数を使用するだけのかなり単純なメソッドを次に示します。

std::string strdate = "2012-06-25 05:32:06.963";
size_t pos = strdate.find_first_not_of("1234567890");
while (pos != std::string::npos)
{
    size_t endpos = strdate.find_first_of("1234567890", pos);
    strdate.erase(pos, endpos - pos);
    pos = strdate.find_first_not_of("1234567890");
}

これは非常に効率的なアプローチではありませんが、機能します。

おそらくより効率的なアプローチは、文字列ストリームを使用する可能性があります...

std::string strdate = "2012-06-25 05:32:06.963";

std::stringstream out;

for (auto i = strdate.begin(); i != strdate.end(); i++)
    if (std::isdigit(*i)) out << *i;

strdate = out.str();

時間や空間の複雑さについては約束しstring::eraseませんが、複数回使用すると、メモリのシャッフルが少し増えるのではないかと思います。

于 2012-06-26T10:09:04.977 に答える
0
std::string strdate = "2012-06-25 05:32:06.963";
std::string result ="";
for(std::string::iterator itr = strdate.begin(); itr != strdate.end(); itr++)
{
    if(itr[0] >= '0' &&  itr[0] <= '9')
    {
        result.push_back(itr[0]);
    }
}
于 2012-06-26T10:28:01.883 に答える