本文整理汇总了C++中std::string::capacity方法的典型用法代码示例。如果您正苦于以下问题:C++ string::capacity方法的具体用法?C++ string::capacity怎么用?C++ string::capacity使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类std::string
的用法示例。
在下文中一共展示了string::capacity方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: FileToString
int FileToString( const char* filename, std::string& out_contents )
{
FILE *fp;
fp = fopen(filename,"r");
if (!fp)
{
printf("Unable to open file '%s' for read into string (%s, %d)\n",filename, __FILE__, __LINE__);
return 1;
}
const size_t capacity_increment = 512;
try
{
out_contents.clear();
out_contents.reserve(capacity_increment);
for (int this_char = fgetc(fp); this_char != EOF; this_char = fgetc(fp))
{
//embiggenate the buffer, if needed
if(out_contents.capacity() == out_contents.size())
{
out_contents.reserve(out_contents.capacity() + capacity_increment);
}
out_contents.push_back(static_cast<char>(this_char));
}
}
catch (const std::exception& e)
{
printf("caught exception '%s' while reading file '%s' into string (%s, %d)\n",e.what(), filename, __FILE__, __LINE__);
return 1;
}
return 0;
}
示例2: load_file
void load_file(std::string& s, std::istream& is)
{
s.erase();
if(is.bad()) return;
s.reserve(static_cast<std::string::size_type>(is.rdbuf()->in_avail()));
char c;
while(is.get(c))
{
if(s.capacity() == s.size())
s.reserve(s.capacity() * 3);
s.append(1, c);
}
}
示例3: while
void Dow::Handle::GetClassName (std::string & name) const
{
// Revisit: there is better API RealGetWindowClass, but it is supported
// only in Win98, NT 4.0 with SP3 and Win2000
unsigned lenAvailable = 128;
unsigned lenWritten = 0;
do
{
name.reserve (lenAvailable); // could reserve more
lenWritten = ::GetClassName (H (), writable_string (name), name.capacity ());
lenAvailable = 2 * name.capacity ();
} while (lenWritten + 1 == name.capacity ());
}
示例4: load_file1
void load_file1(std::string& s, std::istream& is)
{
s.erase(); //删除字符串
//bad() 如果出现错误则返回true
if(is.bad()) return;
s.reserve(is.rdbuf()->in_avail()); //reserve 预留空间
char c;
while(is.get(c))
{ //capacity 在不重新分配内存的情况下,字符串可能的大小 size 得到字符串的大小
if(s.capacity() == s.size())
s.reserve(s.capacity() * 3);
s.append(1, c); //append 追加字符
}
}
示例5: vStringFormat
// original source:
// https://github.com/facebook/folly/blob/master/folly/String.cpp
//
void vStringFormat(std::string& output, const char* format, va_list args)
{
va_list tmpargs;
va_copy(tmpargs,args);
int characters_used = vsnprintf(nullptr, 0, format, tmpargs);
va_end(tmpargs);
if (characters_used < 0) {
// Looks like we have an invalid format string.
// error out.
std::string errorMessage("Invalid format string; snprintf returned negative with format string: ");
errorMessage.append(format);
throw std::runtime_error(errorMessage);
}
else if ((unsigned int)characters_used > output.capacity()) {
output.resize(characters_used+1);
va_copy(tmpargs,args);
characters_used = vsnprintf(&output[0], output.capacity(), format, tmpargs);
va_end(tmpargs);
if (characters_used < 0) {
// We shouldn't have a format error by this point, but I can't imagine what error we
// could have by this point. Still, error out and report it.
std::string errorMessage("Invalid format string or unknown vsnprintf error; vsnprintf returned negative with format string: ");
errorMessage.append(format);
throw std::runtime_error(errorMessage);
}
}
else {
output.resize(characters_used + 1);
va_copy(tmpargs,args);
characters_used = vsnprintf(&output[0], output.capacity(), format, tmpargs);
va_end(tmpargs);
if (characters_used < 0) {
// We shouldn't have a format error by this point, but I can't imagine what error we
// could have by this point. still error out and report it.
std::string errorMessage("Invalid format string or unknown vsnprintf error; vsnprintf returned negative with format string: ");
errorMessage.append(format);
throw std::runtime_error(errorMessage);
}
}
}
示例6: escape
std::string escape(const std::string str, bool intag = true)
{
std::string ret = str;
ret.reserve(str.capacity() * 2);
for (const char c : str) {
if (c == '<') {
ret += "<";
}
else if (c == '>') {
ret += ">";
}
else if (c == '&') {
ret += "&";
}
else if (intag) {
if (c == '\'') {
ret += "&abuff;";
}
else if (c == '\"') {
ret += """;
}
else {
ret += c;
}
}
else {
ret += c;
}
}
return ret;
}
示例7: EscapeForHTML
std::string EscapeForHTML(const std::string& str)
{
typedef std::map<char, std::string> EscapeMap;
static const EscapeMap kHtmlEscapeMap {
{ '<', "<" },
{ '>', ">" },
{ '&', "&" },
{ '"', """ },
{ '\'', "'" },
};
std::string strResult;
strResult.reserve(str.capacity());
for (auto v : str)
{
auto i = kHtmlEscapeMap.find(v);
if (i == kHtmlEscapeMap.end())
{
strResult.push_back(v);
continue;
}
strResult.append(i->second);
}
return strResult;
}
示例8: encode
void encoder::encode(std::string& s) {
s.resize(std::max(s.capacity(), size_t(1))); // Use full capacity, ensure not empty
size_t size = s.size();
if (!encode(&s[0], size)) {
s.resize(size);
encode(&s[0], size);
}
}
示例9: load_file
void load_file(std::string& s, std::istream& is)
{
s.erase();
//
// attempt to grow string buffer to match file size,
// this doesn't always work...
s.reserve(is.rdbuf()->in_avail());
char c;
while(is.get(c))
{
// use logarithmic growth stategy, in case
// in_avail (above) returned zero:
if(s.capacity() == s.size())
s.reserve(s.capacity() * 3);
s.append(1, c);
}
}
示例10: write
void write(std::string& s, const char* from, const char* to) {
assert(from <= to);
const size_t addCount = to - from;
if ( s.capacity() <= s.size() + addCount )
{
s.reserve( 2 * s.size() + addCount );
}
s.append(from, to);
}
示例11: Getline
void TCPSocket::Getline(std::string& buffer, bool stripCRLF)
{
buffer.clear();
buffer.reserve(defaultBufferSize);
char ch;
do
{
ch = GetcharBuffered();
if (!stripCRLF || (ch != '\r' && ch != '\n'))
{
buffer += ch;
if (buffer.length() == buffer.capacity())
buffer.reserve((buffer.capacity() / defaultBufferSize) + 1);
}
}
while (ch != '\n');
}
示例12: ConvertToLower
static std::string ConvertToLower(std::string const& inString)
{
std::string outString;
outString.reserve(inString.capacity());
for (uint32_t i=0; i<inString.length(); i++) {
outString+=tolower(inString[i]);
}
return outString;
}
示例13: func
void func (std::string a, std::wstring b)
{
printf ("%s\n", a.c_str());
printf ("%d\n", a.size());
printf ("%d\n", a.capacity());
wprintf (L"%s\n", b.c_str());
printf ("%d\n", b.size());
printf ("%d\n", b.capacity());
};
示例14: ShowAndGetNewPassword
bool ShowAndGetNewPassword(
std::string& strNewPassword,
unsigned int autoCloseMs)
{
strNewPassword.resize(1024);
unsigned int size = (unsigned int)strNewPassword.capacity();
bool ret = g_interProcess.m_Callbacks->GUI.Dialogs.Keyboard.ShowAndGetNewPassword(strNewPassword[0], size, autoCloseMs);
strNewPassword.resize(size);
strNewPassword.shrink_to_fit();
return ret;
}
示例15:
uint32_t
MemoryConsumption::getStringMemoryUsage(const std::string& s, uint32_t& uniqueCount) {
++_totalStringCount;
const char* internalString = s.c_str();
if (_seenStrings->find(internalString) != _seenStrings->end()) {
return 0;
}
++uniqueCount;
_seenStrings->insert(internalString);
return s.capacity();
}