C ++文本冒险游戏 - 使单词大写

时间:2016-06-26 13:31:31

标签: c++ text vector adventure

基本上,我正在学习本教程:http://cplussplussatplay.blogspot.com.cy/2012/11/text-adventure-games-c-part-1.html

到目前为止我已经了解了所有内容,除此之外:

// Make words upper case
// Right here is where the functions from cctype are used
    for(i = 0; i < words.size(); i++)
    {
        for(j = 0; j < words.at(i).size(); j++)
        {
            if(islower(words.at(i).at(j)))
            {
                words.at(i).at(j) = toupper(words.at(i).at(j));
            }
        }
    }

此时,我们有一个充满字符的单词向量。 我不明白需要两个for循环,也不需要这个单词.at(i).at(j))。 它是2D矢量还是什么?

提前致谢。

编辑:非常感谢你们的帮助!我现在明白了!这是第一次使用Stack Overflow,到目前为止我很喜欢它! :)

还有一件事,又出现了另一个问题!

string sub_str;
vector words;
char search = ' ';



    // Clear out any blanks
    // I work backwords through the vectors here as a cheat not to       invalidate the iterator
    for(i = words.size() - 1; i > 0; i--)
    {
        if(words.at(i) == "")
        {
            words.erase(words.begin() + i);
        }

1。第二条评论意味着什么?

2.载体中是否有空白?根据创建者的第一个循环清除任何空白。 这是以前的代码:

for(i = 0; i < Cmd.size(); i++)
    {
        if(Cmd.at(i) != search)
        {
            sub_str.insert(sub_str.end(), Cmd.at(i));
        }
        if(i == Cmd.size() - 1)
        {
            words.push_back(sub_str);
            sub_str.clear();
        }
        if(Cmd.at(i) == search)
        {
            words.push_back(sub_str);
            sub_str.clear();
        }
    }

再次感谢!

3 个答案:

答案 0 :(得分:2)

我会添加评论来浏览代码:

// Make words upper case
// Right here is where the functions from cctype are used
for(i = 0; i < words.size(); i++) // for each word in the vector "words"
{
    for(j = 0; j < words.at(i).size(); j++) // for each character in the word "words[i]"
    {
        if(islower(words.at(i).at(j)))      // if the character is lower case...
        {
            words.at(i).at(j) = toupper(words.at(i).at(j)); // ...make upper case
        }
    }
}

所以外部循环迭代每个单词,然后内部循环遍历当前单词的每个字符,并将其更改为大写,如果它是小写字符。

我认为将字符更改行更加有效:

words.at(i).at(i) -= 32; // ...make upper case

不需要函数调用:跳转,推送和弹出机器指令以调用toupper()。只允许在同一个函数内进行立即寻址(因此堆栈帧)。

答案 1 :(得分:1)

public static Iterable<Path> shortestPath(Node sourceNode, Node destNode) { Integer depth=100; PathExpander<?> expander; List<Path> paths = new ArrayList<>(); if ( orderedRelTypes == null ) { expander = PathExpanders.allTypesAndDirections(); } else { PathExpanderBuilder expanderBuilder = PathExpanderBuilder.empty(); expanderBuilder = expanderBuilder .add(MyRelationshipTypes.BELONGS_TO, Direction.BOTH) .add(MyRelationshipTypes.CARRIES, Direction.BOTH) .add(MyRelationshipTypes.IS_LOCATED_AT, Direction.BOTH); expander = expanderBuilder.build(); } try (Transaction tx = graphDb.beginTx()) { PathFinder<Path> shortestPath = GraphAlgoFactory.shortestPath( expander, depth == null ? 100 : depth.intValue()); Iterable<Path> pathss = shortestPath.findAllPaths(sourceNode, destNode); for(Path path1:pathss){ paths.add(path1); System.out.println("result::::"+paths); } tx.success(); } return paths; } } 是某个单词的索引。 i是某个字符的索引。 该算法循环遍历单词的每个j,然后处理下一个单词。

答案 2 :(得分:1)

第一个循环通过你的word向量(从第一个单词到位置word.size - 1的最后一个单词)进行迭代,然后第二个循环通过所有单词charaters进行迭代。如果位置j中单词i位置的字符为小写,则将其设为大写