0

I am trying to grab sub-strings out of a larger string and I have got it to work in a small program but when I try to run it into the real program it just goes wrong. I am building off someone else s function and got it to work for my purpose, but cannot get it to work in the main program I need it in. I will limit the program down to where I think error is occurring.

Problem: I pass in same value into function findStats(std::string sString) but get different results. Case I:

stats = findStats("^9dff9d[Attribute 0% Active Defense 0]\r^f2f3f2Mana: 1411 ^00ff00(+1975)\r^f2f3f2^9dff9d[Attribute 0% Active Mana 0]\r^f2f3f2^ffc000Fortify Level: 12/12\r^f2f3f2^006effIdentified Attribute: + 6% Crit Damage\rIdentified Attribute: + 6  Accuracy\r^f2f3f2^006eff^O053Sacrifice Elapse(6/8)\r^00ff00  ^O041Desollar's Shadow\rÌÌÌÌÌÌÌÌL«");

The above case will output correctly and stores \r offsets correctly.

Case II:

stats = findStats((std::string)((char*)&buffer));

Case II is the case I need to work and has the same value as above Case I at start of function findStats but offsets for \r Are not stored for w.e reason when sString has same value at start of function.

    //Function that finds positioning of \r

void calc_z (std::string &s, std::vector<int> & z)
{
int len = s.size();
z.resize (len);

int l = 0, r = 0;
for (int i=1; i<len; ++i)
    if (z[i-l]+i <= r)
        z[i] = z[i-l];
    else
    {
        l = i;
        if (i > r) r = i;
        for (z[i] = r-i; r<len; ++r, ++z[i])
            if (s[r] != s[z[i]])
                break;
        --r;
    }
}


std::vector<std::string> findStats(std::string sString){
//sString is exactly the same in value for both cases of stats at this point
int offSet = 0;
int sOffsets[100] = {};

std::vector<std::string> t1;
std::string main_string = sString;
std::string substring = "\r";
std::string working_string = substring + main_string;
std::vector<int> z;
calc_z(working_string, z);

for(int i = substring.size(); i < working_string.size(); ++i){
    if(z[i] >=substring.size()){
        sOffsets[offSet] = i;
        offSet++;
    }
}
.... code ....problem occurs right above offsets are not stored for \r
}

void main()
{   
std::vector<std::string> stats;
std::string buffer[10];

    ...code...
    ...code to find string and store in buffer...



stats = findStats((std::string)((char*)&buffer));
//stats = findStats("^9dff9d[Attribute 0% Active Defense 0]\r^f2f3f2Mana: 1411 ^00ff00(+1975)\r^f2f3f2^9dff9d[Attribute 0% Active Mana 0]\r^f2f3f2^ffc000Fortify Level: 12/12\r^f2f3f2^006effIdentified Attribute: + 6% Crit Damage\rIdentified Attribute: + 6  Accuracy\r^f2f3f2^006eff^O053Sacrifice Elapse(6/8)\r^00ff00  ^O041Desollar's Shadow\rÌÌÌÌÌÌÌÌL«");
for( std::vector<std::string>::const_iterator i = stats.begin(); i != stats.end(); ++i)std::cout << *i << ' ' << std::endl;

std::cin.get();

}
4

1 回答 1

0

这句话:(std::string)((char*)&buffer)不做你认为它做的事。

  1. std::vector不是一个简单的数组。
  2. 如果您取 的地址std::vector,那将不是 中第一个元素的地址std::vector
  3. 您不能只转换const char*char*转换为 std::string。但是,您可以std::string使用提供的const char*char *c 样式的字符串构造 new。const char *str = asdf; std::string s = std::string(str);.

所以,总结一下:

  1. 如果要一次传递多个字符串std::vector,请通过 const 引用传递缓冲区

    typedef std::vector<std::string> StringVector;
    
    void test(const StringVector& v){
       for (StringVector::const_iterator i = v.begin(); i != v.end(); i++)
           std::cout << *i << std::endl;
    }
    
    ...
    
    StringVector strings;
    test(strings);
    
  2. 如果您想在 WRITE 中写入std::vector内容,请通过引用传递:

    typedef std::vector<std::string> StringVector;
    
    void test(const StringVector& out){
       out.push_back("test");
    }
    
    ...
    
    StringVector strings;
    test(strings);
    
  3. 如果您想从向量传递单个字符串,只需传递元素本身(通过引用、常量引用或按值,具体取决于您要对其执行的操作),而不进行强制转换。

    typedef std::vector<std::string> StringVector;
    
    void test(const std::string& s){
       std::cout << s << std::endl;
    }
    
    ...
    
    StringVector strings;
    strings.push_back("test");
    test(strings[0]);
    

- 编辑 -

在此之上:

std::vector<std::string> findStats(std::string sString){
//sString is exactly the same in value for both cases of stats at this point
int offSet = 0;
int sOffsets[100] = {};//<<here's a problem

在这种情况下使用固定大小的数组是个坏主意。你的数组很小,它会在任何大于 100 字节的字符串上溢出,破坏/崩溃你的程序。根据您的目标,您可以简单地将结果存储在std::vectro<std::string>、制作结构向量或使用。std::map

于 2013-11-11T07:30:29.670 回答