当前位置: 首页>>代码示例>>C++>>正文


C++ string::capacity方法代码示例

本文整理汇总了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;
}
开发者ID:ProframFiles,项目名称:CPSC314-Project-1,代码行数:31,代码来源:cs314_util.cpp

示例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);
   }
}
开发者ID:LancelotGHX,项目名称:Simula,代码行数:13,代码来源:regex_replace_example.cpp

示例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 ());
}
开发者ID:BartoszMilewski,项目名称:WinLib,代码行数:13,代码来源:Win.cpp

示例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 追加字符 
   }
}
开发者ID:KnowNo,项目名称:test-code-backup,代码行数:14,代码来源:regex_merge_example.cpp

示例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);
		}
		
	}
}
开发者ID:Corysia,项目名称:Server,代码行数:52,代码来源:StringUtil.cpp

示例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 += "&lt;";
        }
        else if (c == '>') {
            ret += "&gt;";
        }
        else if (c == '&') {
            ret += "&amp;";
        }
        else if (intag) {
            if (c == '\'') {
                ret += "&abuff;";
            }
            else if (c == '\"') {
                ret += "&quot;";
            }
            else {
                ret += c;
            }
        }
        else {
            ret += c;
        }
    }
    return ret;
}
开发者ID:undisbeliever,项目名称:untech-editor,代码行数:33,代码来源:xml.cpp

示例7: EscapeForHTML

std::string EscapeForHTML(const std::string& str)
{
    typedef std::map<char, std::string> EscapeMap;
    static const EscapeMap kHtmlEscapeMap {
        { '<', "&lt;" },
        { '>', "&gt;" },
        { '&', "&amp;" },
        { '"', "&quot;" },
        { '\'', "&#39;" },
    };

    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;
}
开发者ID:whrool,项目名称:Net,代码行数:25,代码来源:escape.cpp

示例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);
    }
}
开发者ID:Barba-studio,项目名称:qpid-proton,代码行数:8,代码来源:encoder.cpp

示例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);
   }
}
开发者ID:kyhhdm,项目名称:TPlatform,代码行数:17,代码来源:TestReg.cpp

示例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);
 }
开发者ID:JackieXie168,项目名称:loki,代码行数:9,代码来源:SafeFormat.cpp

示例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');
}
开发者ID:arrrrrrr,项目名称:ebftpd,代码行数:18,代码来源:tcpsocket.cpp

示例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;
}
开发者ID:nerdherd,项目名称:frc-2012,代码行数:9,代码来源:CSVReader.cpp

示例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());
};
开发者ID:dennis714,项目名称:tracer,代码行数:10,代码来源:1.cpp

示例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;
 }
开发者ID:MaveriQ,项目名称:xbmc,代码行数:11,代码来源:AddonLib_GUI_DialogKeyboard.cpp

示例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();
}
开发者ID:songhtdo,项目名称:vespa,代码行数:11,代码来源:memoryconsumption.cpp


注:本文中的std::string::capacity方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。