【问题标题】:C++ pushing back a string by value in a vectorC ++按向量中的值推回字符串
【发布时间】:2023-03-23 15:12:01
【问题描述】:

我正在做一些算法问题,但在管理 C++ 中的函数、向量和字符串时遇到了麻烦。

我必须在矩阵中找到一个具体的路径,为此,我需要拥有所有不同的路径,所以我决定使用一个函数。此功能将检查继续搜索的位置。函数代码如下:

vector<string> get_all_paths(string actual, int rows, int columns, int col_actual, int row_prev) {
    vector<string> solutions;
    int row_actual = (rows+row_prev-1)%rows;

    for (int i = 0; i < 3; i++) {

        cout << col_actual << " " << row_actual << "\n";
        //cout << (dp[row_prev][col_actual+1] - matrix[row_prev][col_actual+1]) << " " << dp[row_actual][col_actual] << "\n";
        if( (dp[row_prev][col_actual+1] - matrix[row_prev][col_actual+1]) == dp[row_actual][col_actual] ) {

            if (col_actual > 0) {

                //cout << "--" << actual << " " << row_actual << "\n";
                string branch = actual + to_string(row_actual+1) + " ";
                solutions = get_best_path(branch, rows, columns, col_actual-1, row_actual);
                //cout << ".." << actual << "\n"; 

            } else {

                //cout << actual << " " << row_actual << "\n";
                cout << actual << "\n";
                string branch = actual.c_str();
                branch += to_string(row_actual+1);
                cout << branch << "\n";
                solutions.push_back( branch );
                break;

            }

        }
        row_actual = (row_actual+1)%rows;

    }
    for(auto i : solutions) cout << "--" << i << "\n";

    return solutions;

}

这里是对方法的调用:

vector<string> solutions;
for (int i = 0; i < rows; i++) {

    if (dp[i][cols-1] == min_path) {

        cout << "................\n";
        solutions = get_best_path( (to_string(i+1)+" "), rows, cols, cols-2, i);
        for(auto i : solutions) {
            reverse(i.begin(), i.end());
            cout << i << "\n";
        }
        cout << "xxxxxxxxxxxxxxx\n";

    }

}

问题是我得到了给定示例的三个路径,这是正确的,但它们都是相同的字符串,这是最后一个路径或变量 branch 中所做的最后一个更改.

也许我混合了很多概念,也许这已经被回答了很多次,但我已经搜索过这个并没有得到任何东西。

编辑:我没有从函数中得到三个路径,只是最后一个,但是在函数内部打印时,我在函数内部确实有三个路径都具有相同的值,对此感到抱歉。

EDIT2:问题的想法是在给定矩阵中找到最小成本路径,如果有超过 1 个,则按字典顺序找到最小的路径。所以给定一个矩阵:

5 4

9 1 9 9

1 9 9 9

9 9 9 9

1 1 1 1

9 9 1 9

我的方法是使用 dp 作为具有动态编程结果的矩阵,然后我尝试在上面的函数中重新创建所有路径。

在这种情况下,dp矩阵是:

9 2 11 12

1 10 11 20

9 10 11 12

1 2 3 4

9 10 3 12

因此,最好的路径是:

4 4 4 4

4 5 4 4

2 1 5 4

最后一个是正确的。

在我的函数中,我确实得到了不同的路径,并将它们添加到结果向量中,但是在搜索更多时我会丢失它们。

感谢您抽出宝贵时间阅读本文:D!

【问题讨论】:

  • 与您的问题完全无关,但这是什么string branch = actual.c_str();?只需这样做string branch = actual;
  • 添加 &amp; 到反向:for(auto&amp; i : solutions) { reverse(i.begin(), i.end()); ... }
  • @john 我已经这样做了,但我认为可能 c_str() 给出了字符串的值,而直接赋值给出了引用,但它也没有用。
  • @user3365922 没用,但感谢您的评论,我注意到向量最后只得到 1 个字符串。
  • 一个名为get_best_path() 的函数应该返回一个路径。也许get_best_paths()? “最好”也很模糊。您的参数名称也不容易弄清楚。最后,std::strings 不是一个非常有用的结构来保留您的部分路径。

标签: c++ string c++11 vector


【解决方案1】:

认为问题就在这里

solutions = get_best_path(branch, rows, columns, col_actual-1, row_actual);

因为该分配替换了您迄今为止可能找到的任何解决方案。相反,您应该将返回的任何解决方案附加到您迄今为止找到的任何解决方案中。换句话说就是这样的

vector<string> tmp = get_best_path(branch, rows, columns, col_actual-1, row_actual);
solutions.insert(solutions.end(), tmp.begin(), tmp.end());

但这只是直觉,我没有测试任何东西。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-22
    • 2017-10-03
    • 1970-01-01
    • 2011-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多