64,637
社区成员
发帖
与我相关
我的任务
分享
// ConsoleApplication3.cpp : 定义控制台应用程序的入口点。
//
#define _SCL_SECURE_NO_WARNINGS
#include "iostream"
#include "string"
using namespace std;
void TestCSTR()
{
string str = "zengraoli";
char *test = new char[str.length() + 1];
strcpy(test, str.c_str());
cout << "test length : " << strlen(test) << endl;
puts(test);
if (test)
{
delete[] test;
test = NULL;
}
}
void TestData()
{
string str = "zengraoli";
char *test = new char[str.length() + 1];
strcpy(test, str.data());
cout << "test length : " << strlen(test) << endl;
puts(test);
if (test)
{
delete[] test;
test = NULL;
}
}
void TestCopy()
{
string str = "zengraoli";int i = str.length();
char *test = new char[str.length() + 2];
size_t copy_length = str.copy(test, str.length(), 0);
test[copy_length] = '\0';
cout << "test length : " << strlen(test) << endl;
puts(test);
if (test)
{
delete[] test;
test = NULL;
}
}
int main()
{
// TestCSTR();
// TestData();
// TestCopy();
return 0;
}
void TestCSTR()
{
string str = "zeng\0raoli";int i = str.length();
char *test = new char[str.length() + 1];
memset(test, 0, str.length());
memcpy(test, str.c_str(), str.length());
test[str.length()] = '\0';
cout << "test length : " << strlen(test) << endl;
puts(test);
if (test)
{
delete[] test;
test = NULL;
}
}
void TestCSTR()
{
string str = "zeng\0raoli";int i = str.length();
char *test = new char[11];
memset(test, 0, 11);
memcpy(test, str.c_str(), 10);
test[10] = '\0';
cout << "test length : " << strlen(test) << endl;
puts(test);
if (test)
{
delete[] test;
test = NULL;
}
}
void TestCSTR()
{
string str = "zeng\0raoli";int i = str.length();
char *test = new char[11];
memset(test, 0, 11);
memcpy(test, str.c_str(), 10);
test[10] = '\0';
// cout << "test length : " << strlen(test) << endl;
// puts(test);
for (int index = 0; index < 11; index++)
{
cout << test[index];
}
cout << "\n";
if (test)
{
delete[] test;
test = NULL;
}
}
void TestCSTR()
{
string str = "zeng\0raoli";int i = str.length();
char *test = new char[11];
memset(test, 0, 11);
// memcpy(test, str.c_str(), 10);
for (int index = 0; index < 11; index++)
{
test[index] = *(str.c_str() + index);
}
test[10] = '\0';
// cout << "test length : " << strlen(test) << endl;
// puts(test);
for (int index = 0; index < 11; index++)
{
cout << test[index];
}
cout << "\n";
if (test)
{
delete[] test;
test = NULL;
}
}
Bitset
A bitset stores bits (elements with only two possible values: 0 or 1, true or false, ...).
The class emulates an array of bool elements, but optimized for space allocation: generally, each element occupies only one bit (which, on most systems, is eight times less than the smallest elemental type: char).
Each bit position can be accessed individually: for example, for a given bitset named foo, the expression foo[3] accesses its fourth bit, just like a regular array accesses its elements. But because no elemental type is a single bit in most C++ environments, the individual elements are accessed as special references type (see bitset::reference).
Bitsets have the feature of being able to be constructed from and converted to both integer values and binary strings (see its constructor and members to_ulong and to_string). They can also be directly inserted and extracted from streams in binary format (see applicable operators).
The size of a bitset is fixed at compile-time (determined by its template parameter). For a class that also optimizes for space allocation and allows for dynamic resizing, see the bool specialization of vector (vector<bool>).
// -------------------------------------------------------------------- //
// --- string class --------------------------------------------------- //
// -------------------------------------------------------------------- //
// dynamic string class with thread-safe ref-counted buffer
struct _strrec
{
int refcount;
int length;
};
可以看到string本来就是有默认的length,如果不给他注明长度,他自己会计算你给他赋值的字符串的长度,其实也是用的strlen:
inline int hstrlen(const char* p) // some Unix systems do not accept NULL
{ return p == nil ? 0 : (int)strlen(p); }
strlen其实也是这样的行为:
size_t __cdecl strlen (
const char * str
)
{
const char *eos = str;
while( *eos++ ) ;
return( eos - str - 1 );
}
只要运用得当,string也还是能够存放二进制数据的,重点就是在上面的计算length的长度上面string str = "zeng\0raoli";
都换成 std::string str("zeng\0raoli", 11);
再试试