C++ string类insert函数的用法

请问一下这个函数的具体用法
void set_about(string about)
{
for(int i = 0;i < (int)(about.length() / 2) / 30 && i < 25;i++)
for(int j = 0;j <= 29;j++)
{
if(about.substr(i * 30 * 2 + j * 2,2) == LINEFEED)
about.insert(i * 30 * 2 + j * 2,BLANK,0,2);
set_pixel(i + 8,j + 8,about.substr(i * 30 * 2 + j * 2,2));
}
for(int i = 0;i < (about.length() / 2) % 30;i++)
set_pixel((int)(about.length() / 2) / 30 + 8,i + 8,about.substr((int)((about.length() / 2) / 30) * 30 * 2 + i * 2,2));
}
以上是我所写的一个函数,其中set_pixel()是用于打点的,在此之前此函数并未出现故障 而LINEFEED 和 BLANK是字符串常量,
程序运行到这里的时候出现故障 但是并非是调试出错 而是运行之后程序不能继续运行有点像“无响应”

string的成员函数insert有以下多种重载:

string &insert(int p0, const char *s);——在p0位置插入字符串s

string &insert(int p0, const char *s, int n);——在p0位置插入字符串s的前n个字符

string &insert(int p0,const string &s);——在p0位置插入字符串s

string &insert(int p0,const string &s, int pos, int n);——在p0位置插入字符串s从pos开始的连续n个字符

string &insert(int p0, int n, char c);//在p0处插入n个字符c

iterator insert(iterator it, char c);//在it处插入字符c,返回插入后迭代器的位置

void insert(iterator it, const_iterator first, const_iteratorlast);//在it处插入从first开始至last-1的所有字符

void insert(iterator it, int n, char c);//在it处插入n个字符c

以下是第二行性能的举例代码:

//#include "stdafx.h"//If the vc++6.0, with this line.
#include <string>
#include <iostream>
using namespace std;//
int main(void){
    string a="1234567890",b="abcdefghijklmn";
    a.insert(3,b,5,4);
    cout << a << endl;
    return 0;
}

输出是123fghi4567890.

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-08-06
string-》c风格字符串要转换的。成员函数c_str();本回答被提问者采纳
第2个回答  2013-08-06
set_pixel(i + 8,j + 8,about.substr(i * 30 * 2 + j * 2,2))参数匹配吗??看看其用法,还有参数类型。追问

这个函数是匹配了的,是insert函数的那个位置出错

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网