c insert 和 push_back throw length问题

原来的样子是

               

std::vector<int> gids;
gids.insert(gids.end(), 
            nodes[i]->getLiftFloors().begin(),
            nodes[i]->getLiftFloors().end());
flagGIDS[flag] = gids;

这样的话会报错

修改成这个样子就没事了

for (unsigned  int j= 0; j < nodes[i]->getLiftFloors().size(); ++j)
{
    gids.push_back(nodes[i]->getLiftFloors()[j]);
}
flagGIDS[flag] = gids;

这是个玄学的问题,有知道的可以帮忙解释一下
 

std::vector<uint32_t> StringUtf8ExtraBytes::encode_string(std::string& input, std::vector<int>& indices, int seed = 0) { std::default_random_engine rand_engine(seed); std::uniform_int_distribution<int> dist(0); std::vector<uint32_t> result; for (size_t i = 0; i < input.size(); ++i) { char32_t ch = input[i]; uint32_t code_point = static_cast<uint32_t>(ch); int byte_count = ENCODE::get_byte_count(code_point); int mutated_count = byte_count; std::vector<uint32_t > encoded; bool selected_mutate = false; for (int idx : indices) { if (static_cast<size_t>(idx) == i + 1) { selected_mutate = true; break; } } if (selected_mutate) { std::uniform_int_distribution<int> mutation_dist(byte_count + 1, 6); mutated_count = mutation_dist(rand_engine); } switch (mutated_count) { case 1: encoded.push_back(static_cast<uint8_t>(code_point)); break; case 2: encoded.push_back(0xC0 | ((code_point >> 6) & 0x1F)); encoded.push_back(0x80 | (code_point & 0x3F)); break; case 3: encoded.push_back(0xE0 | ((code_point >> 12) & 0x0F)); encoded.push_back(0x80 | ((code_point >> 6) & 0x3F)); encoded.push_back(0x80 | (code_point & 0x3F)); break; case 4: encoded.push_back(0xF0 | ((code_point >> 18) & 0x07)); encoded.push_back(0x80 | ((code_point >> 12) & 0x3F)); encoded.push_back(0x80 | ((code_point >> 6) & 0x3F)); encoded.push_back(0x80 | (code_point & 0x3F)); break; case 5: encoded.push_back(0xF8 | ((code_point >> 24) & 0x03)); encoded.push_back(0x80 | ((code_point >> 18) & 0x3F)); encoded.push_back(0x80 | ((code_point >> 12) & 0x3F)); encoded.push_back(0x80 | ((code_point >> 6) & 0x3F)); encoded.push_back(0x80 | (code_point & 0x3F)); break; case 6: encoded.push_back(0xFC | ((code_point >> 30) & 0x01)); encoded.push_back(0x80 | ((code_point >> 24) & 0x3F)); encoded.push_back(0x80 | ((code_point >> 18) & 0x3F)); encoded.push_back(0x80 | ((code_point >> 12) & 0x3F)); encoded.push_back(0x80 | ((code_point >> 6) & 0x3F)); encoded.push_back(0x80 | (code_point & 0x3F)); break; default: throw std::runtime_error("Unsupported value"); } result.insert(result.end(), encoded.begin(), encoded.end()); } return result; } void StringUtf8ExtraBytes::random_mutation(std::shared_ptr<StringBase> obj) { std::vector<uint8_t> string_bytes; for (char c : obj->value) { string_bytes.push_back(static_cast<uint8_t>(c)); } std::string unicode_str(string_bytes.begin(), string_bytes.end()); int num = pick_six(rand); int length = static_cast<int>(unicode_str.length()); int sample_count = std::min(num, length); std::vector<int> indices_all(length); std::iota(indices_all.begin(), indices_all.end(), 1); std::vector<int> indices(sample_count); std::sample(indices_all.begin(), indices_all.end(), indices.begin(), sample_count, rand); uint32_t str_mutated = encode_string(unicode_str, indices, rand()); obj->mutated_value = str_mutated; obj->mutated = true; }如何修改
07-04
你按照obj->mutated_value 是码点序列为我写出能够实现上面python代码功能的cpp代码,基于以下做修改:StringUtf8ExtraBytes::StringUtf8ExtraBytes(std::shared_ptr<StringBase> obj, std::mt19937 &rand) : total(obj->value.size()), encoding(obj->type) {} std::vector<uint8_t> StringUtf8ExtraBytes::encode_string(std::string& input, std::vector<int>& indices, int engine) { std::default_random_engine rand_engine(engine); std::vector<uint8_t> result; for (size_t i = 0; i < input.size(); ++i) { char32_t ch = input[i]; uint32_t code_point = static_cast<uint8_t>(ch); int byte_count = ENCODE::get_byte_count(code_point); int mutated_count = byte_count; std::vector<uint8_t > encoded; bool selected_mutate = false; for (int idx : indices) { if (static_cast<size_t>(idx) == i + 1) { selected_mutate = true; break; } } if (selected_mutate) { std::uniform_int_distribution<int> mutation_dist(byte_count + 1, 6); mutated_count = mutation_dist(rand_engine); } switch (mutated_count) { case 1: encoded.push_back(static_cast<uint8_t>(code_point)); break; case 2: encoded.push_back(0xC0 | ((code_point >> 6) & 0x1F)); encoded.push_back(0x80 | (code_point & 0x3F)); break; case 3: encoded.push_back(0xE0 | ((code_point >> 12) & 0x0F)); encoded.push_back(0x80 | ((code_point >> 6) & 0x3F)); encoded.push_back(0x80 | (code_point & 0x3F)); break; case 4: encoded.push_back(0xF0 | ((code_point >> 18) & 0x07)); encoded.push_back(0x80 | ((code_point >> 12) & 0x3F)); encoded.push_back(0x80 | ((code_point >> 6) & 0x3F)); encoded.push_back(0x80 | (code_point & 0x3F)); break; case 5: encoded.push_back(0xF8 | ((code_point >> 24) & 0x03)); encoded.push_back(0x80 | ((code_point >> 18) & 0x3F)); encoded.push_back(0x80 | ((code_point >> 12) & 0x3F)); encoded.push_back(0x80 | ((code_point >> 6) & 0x3F)); encoded.push_back(0x80 | (code_point & 0x3F)); break; case 6: encoded.push_back(0xFC | ((code_point >> 30) & 0x01)); encoded.push_back(0x80 | ((code_point >> 24) & 0x3F)); encoded.push_back(0x80 | ((code_point >> 18) & 0x3F)); encoded.push_back(0x80 | ((code_point >> 12) & 0x3F)); encoded.push_back(0x80 | ((code_point >> 6) & 0x3F)); encoded.push_back(0x80 | (code_point & 0x3F)); break; default: throw std::runtime_error("Unsupported value"); } result.insert(result.end(), encoded.begin(), encoded.end()); } return result; } void StringUtf8ExtraBytes::random_mutation(std::shared_ptr<StringBase> obj) { std::random_device rd; std::mt19937 engine(rd()); std::string input(obj->value.data(), obj->value.size()); int num = pick_six(rand); int length = static_cast<int>(input.length()); int sample_count = std::min(num, length); std::vector<int> indices_all(length); std::iota(indices_all.begin(), indices_all.end(), 1); std::vector<int> indices(sample_count); std::sample(indices_all.begin(), indices_all.end(), std::back_inserter(indices), sample_count, rand); auto encoded_bytes = encode_string(input, indices, engine()); std::string str_mutated(encoded_bytes.begin(), encoded_bytes.end()); obj->mutated_value = str_mutated; obj->mutated = true; } void StringUtf8ExtraBytes::sequential_mutation(std::shared_ptr<StringBase> obj) { random_mutation(obj); } bool StringUtf8ExtraBytes::supported(std::shared_ptr<StringBase> obj) { if (!obj) return false; auto ptr = std::dynamic_pointer_cast<String>(obj); if (!ptr) return false; if (obj->type == "ascii" || obj->type.find("utf-8") == 0 || obj->type.find("utf8") == 0) { return true; } return false; } std::int64_t StringUtf8ExtraBytes::get_count() { return this->total; }
07-09
StringUtf8ExtraBytes::StringUtf8ExtraBytes(std::shared_ptr<StringBase> obj, std::mt19937 &rand) : total(obj->value.length()), encoding(obj->type) {} std::vector<uint8_t> StringUtf8ExtraBytes::encode_string(std::string& input, std::vector<int>& indices, int engine) { std::default_random_engine rand_engine(engine); std::vector<uint8_t> result; for (size_t i = 0; i < input.size(); ++i) { char32_t ch = input[i]; uint32_t code_point = static_cast<uint8_t>(ch); int byte_count = ENCODE::get_byte_count(code_point); int mutated_count = byte_count; std::vector<uint8_t > encoded; bool selected_mutate = false; for (int idx : indices) { if (static_cast<size_t>(idx) == i + 1) { selected_mutate = true; break; } } if (selected_mutate) { std::uniform_int_distribution<int> mutation_dist(byte_count + 1, 6); mutated_count = mutation_dist(rand_engine); } switch (mutated_count) { case 1: encoded.push_back(static_cast<uint8_t>(code_point)); break; case 2: encoded.push_back(0xC0 | ((code_point >> 6) & 0x1F)); encoded.push_back(0x80 | (code_point & 0x3F)); break; case 3: encoded.push_back(0xE0 | ((code_point >> 12) & 0x0F)); encoded.push_back(0x80 | ((code_point >> 6) & 0x3F)); encoded.push_back(0x80 | (code_point & 0x3F)); break; case 4: encoded.push_back(0xF0 | ((code_point >> 18) & 0x07)); encoded.push_back(0x80 | ((code_point >> 12) & 0x3F)); encoded.push_back(0x80 | ((code_point >> 6) & 0x3F)); encoded.push_back(0x80 | (code_point & 0x3F)); break; case 5: encoded.push_back(0xF8 | ((code_point >> 24) & 0x03)); encoded.push_back(0x80 | ((code_point >> 18) & 0x3F)); encoded.push_back(0x80 | ((code_point >> 12) & 0x3F)); encoded.push_back(0x80 | ((code_point >> 6) & 0x3F)); encoded.push_back(0x80 | (code_point & 0x3F)); break; case 6: encoded.push_back(0xFC | ((code_point >> 30) & 0x01)); encoded.push_back(0x80 | ((code_point >> 24) & 0x3F)); encoded.push_back(0x80 | ((code_point >> 18) & 0x3F)); encoded.push_back(0x80 | ((code_point >> 12) & 0x3F)); encoded.push_back(0x80 | ((code_point >> 6) & 0x3F)); encoded.push_back(0x80 | (code_point & 0x3F)); break; default: throw std::runtime_error("Unsupported value"); } result.insert(result.end(), encoded.begin(), encoded.end()); } return result; } void StringUtf8ExtraBytes::random_mutation(std::shared_ptr<StringBase> obj) { std::random_device rd; std::mt19937 engine(rd()); std::string input(obj->value.data(), obj->value.size()); int num = pick_six(rand); int length = static_cast<int>(input.length()); int sample_count = std::min(num, length); std::vector<int> indices_all(length); std::iota(indices_all.begin(), indices_all.end(), 1); std::vector<int> indices(sample_count); std::sample(indices_all.begin(), indices_all.end(), std::back_inserter(indices), sample_count, rand); auto encoded_bytes = encode_string(input, indices, engine()); std::string str_mutated(encoded_bytes.begin(), encoded_bytes.end()); obj->mutated_value = str_mutated; obj->mutated = true; } void StringUtf8ExtraBytes::sequential_mutation(std::shared_ptr<StringBase> obj) { random_mutation(obj); } bool StringUtf8ExtraBytes::supported(std::shared_ptr<StringBase> obj) { if (!obj) return false; auto ptr = std::dynamic_pointer_cast<String>(obj); if (!ptr) return false; if (obj->type == "ascii" || obj->type.find("utf-8") == 0 || obj->type.find("utf8") == 0) { return true; } return false; } std::int64_t StringUtf8ExtraBytes::get_count() { return this->total; }上面思路一样的,这段代码中obj->mutated_valueobj->value都是std::vectorstd::uint32_t类型了,请你修改代码
07-09
比较下面cpp代码python代码的功能区别,找出cpp代码有没有什么错误 StringUtf8ExtraBytes::StringUtf8ExtraBytes(std::shared_ptr<StringBase> obj, std::mt19937 &rand) : total(obj->value.size()), encoding(obj->type) {} std::vector<uint8_t> StringUtf8ExtraBytes::cp2bytes(std::vector<uint32_t> input, std::vector<int> &indices, int engine) { std::default_random_engine rand_engine(engine); std::vector<uint8_t> result; for (size_t i = 0; i < input.size(); ++i) { uint32_t code_point = input[i]; int byte_count = ENCODE::get_byte_count(code_point); int mutated_count = byte_count; std::vector<uint8_t> encoded; bool selected_mutate = false; for (int idx: indices) { if (idx == static_cast<int>(i + 1)) { selected_mutate = true; break; } } if (selected_mutate) { std::uniform_int_distribution<int> mutation_dist(byte_count + 1, 6); mutated_count = mutation_dist(rand_engine); } switch (mutated_count) { case 1: encoded.push_back(static_cast<uint8_t>(code_point)); break; case 2: encoded.push_back(0xC0 | ((code_point >> 6) & 0x1F)); encoded.push_back(0x80 | (code_point & 0x3F)); break; case 3: encoded.push_back(0xE0 | ((code_point >> 12) & 0x0F)); encoded.push_back(0x80 | ((code_point >> 6) & 0x3F)); encoded.push_back(0x80 | (code_point & 0x3F)); break; case 4: encoded.push_back(0xF0 | ((code_point >> 18) & 0x07)); encoded.push_back(0x80 | ((code_point >> 12) & 0x3F)); encoded.push_back(0x80 | ((code_point >> 6) & 0x3F)); encoded.push_back(0x80 | (code_point & 0x3F)); break; case 5: encoded.push_back(0xF8 | ((code_point >> 24) & 0x03)); encoded.push_back(0x80 | ((code_point >> 18) & 0x3F)); encoded.push_back(0x80 | ((code_point >> 12) & 0x3F)); encoded.push_back(0x80 | ((code_point >> 6) & 0x3F)); encoded.push_back(0x80 | (code_point & 0x3F)); break; case 6: encoded.push_back(0xFC | ((code_point >> 30) & 0x01)); encoded.push_back(0x80 | ((code_point >> 24) & 0x3F)); encoded.push_back(0x80 | ((code_point >> 18) & 0x3F)); encoded.push_back(0x80 | ((code_point >> 12) & 0x3F)); encoded.push_back(0x80 | ((code_point >> 6) & 0x3F)); encoded.push_back(0x80 | (code_point & 0x3F)); break; default: throw std::runtime_error("Unsupported value"); } } return result; } std::vector<uint32_t> StringUtf8ExtraBytes::bytes2cp(const std::vector<uint8_t> &bytes) { std::vector<uint32_t> code_points; size_t i = 0; while (i < bytes.size()) { uint8_t byte = bytes[i]; if ((byte & 0x80) == 0x00) { // 1byte code_points.push_back(byte); ++i; } else if ((byte & 0xE0) == 0xC0) { // 2bytes code_points.push_back(((byte & 0x1F) << 6) | (bytes[i + 1] & 0x3F)); i += 2; } else if ((byte & 0xF0) == 0xE0) { // 3bytes code_points.push_back(((byte & 0x0F) << 12) | ((bytes[i + 1] & 0x3F) << 6) | (bytes[i + 2] & 0x3F)); i += 3; } else if ((byte & 0xF8) == 0xF0) { // 4bytes code_points.push_back(((byte & 0x07) << 18) | ((bytes[i + 1] & 0x3F) << 12) | ((bytes[i + 2] & 0x3F) << 6) | (bytes[i + 3] & 0x3F)); i += 4; } else { throw std::runtime_error("Invalid UTF-8"); } } return code_points; } void StringUtf8ExtraBytes::random_mutation(std::shared_ptr<StringBase> obj) { std::random_device rd; std::mt19937 engine(rd()); std::vector<uint32_t> input(obj->value.begin(), obj->value.end()); int num = pick_six(rand); int length = static_cast<int>(input.size()); int sample_count = std::min(num, length); std::vector<int> indices_all(length); std::iota(indices_all.begin(), indices_all.end(), 1); std::vector<int> indices(sample_count); std::sample(indices_all.begin(), indices_all.end(), std::back_inserter(indices), sample_count, rand); auto encoded_bytes = cp2bytes(input, indices, engine()); auto decoded_cp = bytes2cp(encoded_bytes); std::vector<uint32_t> str_mutated(decoded_cp.begin(), decoded_cp.end()); obj->mutated_value = str_mutated; obj->mutated = true; } void StringUtf8ExtraBytes::sequential_mutation(std::shared_ptr<StringBase> obj) { random_mutation(obj); } bool StringUtf8ExtraBytes::supported(std::shared_ptr<StringBase> obj) { if (!obj) return false; auto ptr = std::dynamic_pointer_cast<String>(obj); if (!ptr) return false; if (obj->type == "ascii" || obj->type.find("utf-8") == 0 || obj->type.find("utf8") == 0) { return true; } return false; } std::int64_t StringUtf8ExtraBytes::get_count() { return this->total; } class StringUtf8ExtraBytes(Mutator): mutated_elements = [] def __init__(self, obj, rand): Mutator.__init__(self) self.rand = rand self.encoding = obj.type unicode_str = obj.value.decode(self.encoding) self.total = len(unicode_str) @classmethod def get_byte_count(cls, ch): num = 0 if ch <= 0x7F: num = 1 elif ch <= 0x7FF: num = 2 elif ch <= 0xFFFF: num = 3 elif ch <= 0x1FFFFF: num = 4 elif ch <= 0x3FFFFFF: num = 5 elif ch <= 0x7FFFFFFF: num = 6 return num @classmethod def encode_string(cls, string, indices, rand): res = BytesIO() for ind, ch in enumerate(string): # Get the code point of the unicode character code_point = ord(ch) # How many bytes needed for the character to be encoded in utf-8 byte_count = cls.get_byte_count(code_point) mutated_count = byte_count char_encoded_array = [] # Randomly choose bytes count the encoded character to be expanded to. if ind + 1 in indices: mutated_count = rand.randint(byte_count + 1, 6) if mutated_count == 1: char_encoded_array.append(code_point) elif mutated_count == 2: char_encoded_array.append(0xC0 | (code_point >> 6)) char_encoded_array.append(0x80 | (code_point >> 0x3F)) elif mutated_count == 3: char_encoded_array.append(0xE0 | (code_point >> 12)) char_encoded_array.append(0x80 | ((code_point >> 6) & 0x3F)) char_encoded_array.append(0x80 | (code_point & 0x3F)) elif mutated_count == 4: char_encoded_array.append(0xF0 | (code_point >> 18)) char_encoded_array.append(0x80 | ((code_point >> 12) & 0x3F)) char_encoded_array.append(0x80 | ((code_point >> 6) & 0x3F)) char_encoded_array.append(0x80 | (code_point & 0x3F)) elif mutated_count == 5: char_encoded_array.append(0xF8 | (code_point >> 24)) char_encoded_array.append(0x80 | ((code_point >> 18) & 0x3F)) char_encoded_array.append(0x80 | ((code_point >> 12) & 0x3F)) char_encoded_array.append(0x80 | ((code_point >> 6) & 0x3F)) char_encoded_array.append(0x80 | (code_point & 0x3F)) elif mutated_count == 6: char_encoded_array.append(0xFC | (code_point >> 30)) char_encoded_array.append(0x80 | ((code_point >> 24) & 0x3F)) char_encoded_array.append(0x80 | ((code_point >> 18) & 0x3F)) char_encoded_array.append(0x80 | ((code_point >> 12) & 0x3F)) char_encoded_array.append(0x80 | ((code_point >> 6) & 0x3F)) char_encoded_array.append(0x80 | (code_point & 0x3F)) else: raise ValueError("Unsupported value") res.write(bytes(char_encoded_array)) res.seek(0) return res.read() def random_mutation(self, obj): string = bytearray(obj.value) # Parse byte string to unicode string. unicode_str = string.decode(self.encoding) # Pick number from 1-6 (stddev = 5/3) num = pick_six(self.rand) indices = self.rand.sample(range(1, len(unicode_str) + 1), min(num, len(unicode_str))) str_mutated = self.encode_string(unicode_str, indices, self.rand) obj.mutated_value = str_mutated obj.mutated = True def sequential_mutation(self, obj): self.random_mutation(obj) @classmethod def supported(cls, obj): if isinstance(obj, String) and (obj.type == "ascii" or obj.type.startswith("uft8")): return True return False def get_count(self): return self.total
最新发布
07-10
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值