gpt4 book ai didi

c++ - 读出一个字符串流并将其插入不同的 vector 中

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:11:30 25 4
gpt4 key购买 nike

(对不起,如果我问错了,这是我第一次在论坛上写文章)

当我开始在我的 SFML - Game 上编程时,我有一本很旧的书,非常像 C(例如 atoi(); 的推荐)。
现在我得到了一本新的 C++(包括 C++11)书,我想用更新的代码重写旧行。

我将 Tiles 保存在这样存储的文件中:

[0-0,15-1|22,44] [0-1|0]
[4-0,10-1,3-1|0] [0-5,5-5|0]

这意味着:
[...] 描述了一个 Tile
0-0等是Texturesheet上的xy位置
22等是要触发的事件。
事件数量和 sf::Vector2i 不应该经常设置。

Tiles是单独从另一个类中取出来的,这个类管理着整个Tilemap。

现在我的问题是:我不知道应该如何将 strinstream 中的数字推送到两个 vector 中?我的代码:

class Tile{
private:
std::deque<sf::Sprite> tile;
std::deque<int> event;
public:

Tile(sf::Texture& texture, std::deque<sf::Vector2i>&& ctor_texturerects, std::deque<int>&& ctor_events);//This one is working fine

Tile(sf::Texture& texture, std::stringstream&& ctor_stream/*reads the Tile*/){

std::deque<sf::Vector2i> temp_texturerects;
std::deque<int>temp_events;
/*TODO: filter the stringstream and push them into the containers*/
Tile::Tile(texture,std::move(temp_texturerect),std::move(temp_events));
}

如果你能给我另一个解决方案,我也会很高兴,比如将 sf::Vector2i 更改为更好的解决方案,或者给我一个更好的流和类概念
提前致谢
异种 Ceph

编辑:
我做了一些解决方法:
(我把输入流改成了普通字符串)
但是代码看起来不太好必须有一个更简单的解决方案

Tile::  Tile(sf::Texture& texture, std::string&& ctor_string){
std::deque<sf::Vector2i> temp_texturerects;
std::deque<int> temp_events;
std::stringstream strstr;


for(int i=0; i<ctor_string.size(); ++i){
while(ctor_string[i]!='|'){

while(ctor_string[i] != ','){
strstr << ctor_string[i];
}
sf::Vector2i v2i;
strstr >> v2i.x >> v2i.y;
temp_texturerects.push_front(v2i);
strstr.str("");
}
while(ctor_string[i]!=']'){
while(ctor_string[i] != ','){
strstr << ctor_string[i];
}
int integer;
strstr >> integer;
temp_events.push_front(integer);
strstr.str("");
}
}
Tile::Tile(texture, std::move(temp_texturerects), std::move(temp_events));
}

有没有更好的解决方案?

最佳答案

如果我正确理解你的问题,你有一些形式的字符串

[0-0,15-1|22,44] [0-1|0]
[4-0,10-1,3-1|0] [0-5,5-5|0]

并且您想提取 2 种类型的数据 - 位置(例如 0-0)和事件(例如 22)。您的问题是如何干净地提取此数据,丢弃 [] 字符等。

解决这个问题的一个好方法是使用 getline 函数,该函数在字符串流上运行,它继承自 std::istream ( http://www.cplusplus.com/reference/string/string/getline/ )。它可以采用自定义分隔符,而不仅仅是换行符。所以可以使用'[''|'']'作为不同的定界符,按照逻辑顺序解析。

例如,由于您的字符串只是图 block 的集合,您可以将其拆分为多个函数 - ParseTileParsePositionsParseEvents,类似于以下内容:

void Tile::ParseInput(stringstream&& ctor_string) {

//extract input, tile by tile
while(!ctor_string.eof()) {
string tile;

//you can treat each tile as though it is on a separate line by
//specifying the ']' character as the delimiter for the "line"
getline(ctor_string, tile, ']');
tile += "]"; //add back the ']' character that was discarded from the getline

//the string "tile" should now contain a single tile [...], which we can further process using ParseTile
ParseTile(tile);
}

}

ParseTile 函数:

void Tile::ParseTile(string&& tile) {
//input string tile is e.g. " [0-0, 15-1|22,44]"

string discard; //string to hold parts of tile string that should be thrown away
string positions; //string to hold list of positions, separated by ','
string events; //string to hold events, separated by ','

//create stringstream from input
stringstream tilestream(tile);
//tilestream is e.g. "[0-0,15-1|22,44]"

getline(tilestream, discard, '['); //gets everything until '['
//now, discard is " [" and tilestream is "0-0,15-1|22,44]"

getline(tilestream, positions, '|');
//now, positions is "0-0,15-1" and tilestream is "22,44]"

getline(tilestream, events,']');
//now, events is "22,44" and tilestream is empty

ParsePositions(positions);

ParseEvents(events);


}

您可以编写自己的 ParsePositions 和 ParseEvents 函数,它们基本上是使用“,”作为分隔符的更多 getline 调用(只是循环直到字符串结束)。

关于c++ - 读出一个字符串流并将其插入不同的 vector 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15724254/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com