找出文件中所有浮点数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22void ParseFloatData(const char* pFilePath, std::vector<float>& vecFloat)
{
if(!pFilePath)
return;
FILE* pFile = fopen(pFilePath, "rb");
if(!pFile)
return;
fseek(pFile, 0L, SEEK_END);
size_t filesize = ftell(pFile);
rewind(pFile);
std::shared_ptr<std::vector<char>> vecBuffer = std::make_shared<std::vector<char>>(5000000);
fread(vecBuffer->data(),1,filesize,pFile);
fclose(pFile);
std::regex regexFloat("-?(([1-9]\\d*\\.\\d*)|(0\\.\\d*[1-9]\\d*))");
const std::string strFloatData = vecBuffer->data();
for(std::sregex_iterator iter(strFloatData.begin(), strFloatData.end(), regexFloat),end;iter != end; ++iter)
{
vecFloat.push_back(static_cast<float>(stof(iter->str())));
}
}std::shared_ptr 的实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69template<class T>
class shared_ptr
{
public:
shared_ptr(T* ptr == nullptr)
{
m_ptr = ptr;
m_RefCount = new int(1);
m_mtx = new std::mutex();
}
void AddRef()
{
std::lock_guard<std::mutex> lock(*m_mtx);
++(*m_RefCount);
}
void Release()
{
bool releaseFlag = false;
m_mtx->lock();
if(--(*m_RefCount) == 0)
{
delete m_RefCount;
delete m_ptr;
releaseFlag = true;
}
m_mtx->unlock();
if(releaseFlag)
delete m_mtx;
}
shared_ptr(const shared_ptr<T> shptr)
{
m_ptr = shptr.m_ptr;
m_mtx = shptr.m_mtx;
m_RefCount = shptr.m_RefCount;
AddRef();
}
shared_ptr<T>& operator=(shared_ptr<T>& shptr)
{
if(m_ptr != shptr.m_ptr)
{
Release();
m_ptr = shptr.m_ptr;
m_mtx = shptr.m_mtx;
m_RefCount = shptr.m_RefCount;
AddRef();
}
return *this;
}
T& operator*()
{
return *m_ptr;
}
T* operatpr->()
{
return m_ptr;
}
int GetUseCount()
{
return *m_RefCount;
}
private:
T* m_ptr;
int* m_RefCount;
std::mutex *m_mtx;
};std::unique_ptr 的实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32template<class T>
class unique_ptr
{
public:
unique_lock(T* ptr = nullptr)
{
m_ptr = ptr;
}
explicit unique_ptr(unique_ptr<T> && uniptr)
{
this->m_ptr = uniptr.m_ptr;
uniptr.m_ptr = nullptr;
}
unique_ptr<T>& operator=(unique_ptr<T>&& uniptr)
{
if(m_ptr)
delete m_ptr;
m_ptr = uniptr.m_ptr;
uniptr.m_ptr = nullptr;
return *this;
}
T& operator*()
{
return *m_ptr;
}
T* operator->()
{
return m_ptr;
}
private:
T* m_ptr;
};