【问题标题】:For loop incrementer doesn't work as index for vector of stringsFor 循环增量器不能作为字符串向量的索引
【发布时间】:2019-12-06 07:14:12
【问题描述】:

我目前在测试我的冒泡排序时遇到了麻烦(我还没有完成它的实际代码)但是当我有字符串向量时:“words[j][j]”并且它没有print ANYTHING while doing "words[0][0]" 确实会打印一些东西。

#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <stdio.h>

using namespace std;

vector<string>* get_words()
{
    fstream word_file;
    string input;

    vector<string>* retval = new vector<string>();
    word_file.open("word_list.txt", ios::in);

    getline(word_file, input);
    while (word_file) {

        if (input.length() != 0 && input[0] != '#') {
            retval->push_back(input);
            cout << input << endl;
        }

        getline(word_file, input);
    }

    word_file.close();
    return retval;
}

void bubbleList(vector<string>* words)
{
    for (int j = 0; j < words->size() - 1; j++) {
        cout << words[j][j] << endl; //PROBLEM IS HERE
        for (int i = j + 1; i < words->size(); i++) {
        }
    }
}

void printVector(vector<string>* printer)
{
    cout << printer << endl;
}

int main()
{
    vector<string>* wordsList;
    wordsList = get_words();
    bubbleList(wordsList);

    return 0;
}

另外,变量名只是为了让它工作,不用担心。任何帮助表示赞赏:)

【问题讨论】:

  • 你的作业需要使用指针吗?
  • word 是指向vector&lt;string&gt;指针。你需要(*words)[j][j]。但是你真正应该做的是停止动态分配你的向量并传递一个指向它的指针。 get_words 应该按值返回,bubbleList 应该通过引用获取它的参数。

标签: c++ string for-loop vector indexing


【解决方案1】:

words 是一个指针。在指针上使用索引运算符会将其视为数组。例如:

int * i = new int[4];
i[0] = 1; // set the first element of the array
i[1] = 2; // set the second element of the array
i[2] = 3; // set the third element of the array

由于您尚未创建数组,因此您可以访问的唯一索引是第一个元素,这相当于取消引用指针。因此,您的代码应该是:

cout << words[0][j] << "\n";

或更传统的:

cout << (*words)[j] << "\n";

但是,您根本不需要在代码中使用指针。您可以从get_words 按值返回向量,然后通过引用将其传递给其他函数:

#include <iostream>
#include <vector>
#include <fstream>
#include <string>

std::vector<std::string> get_words()
{
    std::fstream word_file;
    std::string input;

    std::vector<std::string> retval;
    word_file.open("word_list.txt", std::ios::in);

    std::getline(word_file, input);
    while (word_file) {

        if (input.length() != 0 && input[0] != '#') {
            retval.push_back(input);
            std::cout << input << "\n";
        }

        std::getline(word_file, input);
    }

    word_file.close();
    return retval;
}

void bubbleList(std::vector<std::string>& words)
{
    for (int j = 0; j < words.size() - 1; j++) {
        std::cout << words[j] << "\n";
        for (int i = j + 1; i < words.size(); i++) {
        }
    }
}

void printVector(const std::vector<std::string>& printer)
{
    for (auto& word : printer)
    {
        std::cout << word << "\n";
    }
}

int main()
{
    std::vector<std::string> wordsList;
    wordsList = get_words();
    bubbleList(wordsList);

    return 0;
}

【讨论】:

    【解决方案2】:

    指向向量的指针指向单个向量。因此,循环中的索引具有未定义的行为:

    for(int j = 0; j < words->size()-1; j++){
            cout << words[j][j]<< endl;             //PROBLEM IS HERE
            for(int i = j+1; i < words->size(); i++){
    
        }
    

    因为你的新:words[0][j]

    不明白为什么你首先需要一个指向向量的指针。

    【讨论】:

      【解决方案3】:

      这里的问题是vector&lt;string&gt;* vec = new vector&lt;string&gt;();vector&lt;string&gt; vec();的动态版本

      这不是二维向量。当您使用 [0][0] 进行索引时,您没有偏移,这就是它显示的原因。与其将其视为 2d,不如将其视为具有第一个偏移量 0 的单维:[0][j]

      这行代码正在创建一个单行单列的向量,该向量只能在列方向上增长。这就是为什么在索引它时必须有 [0][index] 的原因。

      vector<string> *retval = new vector<string>();
      

      如果你需要一个使用指针的二维向量,那么你应该做的是:

      vector<string>* vec = new vector<string>[SIZE]();
      

      现在你有了一个向量,你可以用大于 0 的行值来索引它。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-07-07
        • 2020-02-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多