如何解决为什么Vector数组打印内存地址而不是数组值,为什么在排序函数中出现访问冲突?
我正在开发一个程序,该程序读取文本文件并将单词存储在可以排序然后搜索的数组中。我已经尝试过使用动态分配的数组从文本文件中放入单词,但是我得到的只是字符串无法从我的getline中读取错误(使用向量时不会发生这种情况)。当我尝试打印出向量数组以查看传递给我的排序函数的内容时,它将打印出数组的内存地址,而不是打印出数组中存储的值。
在将向量数组传递给排序函数之后,在排序函数的某些点,我也遇到了读取访问冲突,但我不明白为什么。我确实在代码中显示了问题所在。请注意,我对编码非常陌生,该程序还远远不够完成。我包含了到目前为止已经完成的所有代码,因为如果仅显示问题区域,我认为为什么会有这些错误是不可以理解的。感谢您的任何帮助。
C
头文件
#include "flore0900header.h"
int main()
{
string name;
int num = 0,num2 = 0,count = 0;
vector<string> word; //to pass vector array to another function
cout << "Hello. Enter your name: ";
cin >> name;
cout << endl;
cout << "Welcome " << name << " This program lets you test 5 different sorting algorithms using texts files" << endl;
cout << endl;
cout << "Which text file would you like to search?" << endl;
cout << "=========================================" << endl;
cout << "1. The Blue Hotel" << endl;
cout << "2. 20,000 Leagues Under the Sea" << endl;
cout << "3. A Tale of Two Cities" << endl;
cin >> num;
count = text_select(num);
catch_array(&word); //passing vector array by reference. '&' is there because it won't work otherwise
cout << endl;
cout << "Which sorting algorithems would you like to use?" << endl;
cout << "========================================" << endl;
cout << "1. Selection" << endl;
cout << "2. Bubble" << endl;
cout << "3. Insertion" << endl;
cout << "4. Merge" << endl;
cout << "5. Quick" << endl;
cin >> num; cin >> num2;
sort_select(num,&word,count);//'&' is there because it won't work otherwise
sort_select(num2,count);//'&' is there because it won't work otherwise
cout << endl;
cout << "Ok! Running algorithms..........." << endl;
}
#ifndef FLORE0900HEADER_H
#define FLORE0900HEADER_H
#include <iomanip>
#include <ctime>
#include <cstdlib>
#include <istream>
#include <fstream>
#include <string>
#include <iostream>
#include <sstream>
#include <vector>
#include <ostream>
/* some of the includes are not needed,I haven't removed the un-needed ones yet*/
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::vector;
void selection_sort(vector<string> a[],int size);
void bubble_sort(vector<string> a[],int size);
void insertion_sort(vector<string> a[],int size);
void merge_sort(vector<string> a[],int from,int to);
void quick_sort(vector<string> a[],int to);
int text_select(int num);
void sort_select(int num,vector<string> a[],int size);
vector<string> catch_array(vector<string> a[]);
void merge(vector<string> a[],int mid,int to);
int min_position(vector<string> a[],int to);
int partition(vector<string> a[],int to);
void swap(int& x,int& y);
void print(vector<string> a[],int size);
#endif
sort_select文件
#include "flore0900header.h"
//#include <vector>
int text_select(int num)
{
int count = 0;
vector<string> words(100000);
//vector<string>* word2 = new vector<string>[count];
std::ifstream infile;
if (num == 1)
{
infile.open("blue_hotel.txt");
if (infile.is_open())
{
cout << "file is open" << endl;
getline(infile,words[count]);//string read error when using not using vector
while (!infile.eof())
{
infile >> words[count];
count++;
infile.ignore();
getline(infile,words[count]);
}
}
else
{
cout << "file didn't open" << endl;
exit(1);
}
}
else if (num == 2)
{
infile.open("2under.txt");
if (infile.is_open())
{
cout << "file is open" << endl;
getline(infile,words[count]);
while (!infile.eof())
{
infile >> words[count];
count++;
infile.ignore();
getline(infile,words[count]);
}
}
else
{
cout << "file didn't open" << endl;
exit(1);
}
}
else if (num == 3)
{
infile.open("2city10.txt");
if (infile.is_open())
{
cout << "file is open" << endl;
getline(infile,words[count]);
}
}
else
{
cout << "file didn't open" << endl;
exit(1);
}
}
else
cout << "not a valid choice try again" << endl;
infile.close();
catch_array(&words);//if I don't use '&' the vector won't pass through
return count;
}
vector<string> catch_array(vector<string> a[])
{
return *a;//if I don't put '*' before the 'a' I get an error
}
selection_sort文件
#include "flore0900header.h"
void sort_select(int num,int size)
{
int from = 0;
if (num == 1)
{
selection_sort(a,size);
}
else if (num == 2)
{
bubble_sort(a,size);
}
else if (num == 3)
{
insertion_sort(a,size);
}
else if (num == 4)
{
merge_sort(a,from,size);
}
else if (num == 5)
{
quick_sort(a,size);
}
else
cout << "not a valid pick try again" << endl;
}
bubble_sort文件
#include "flore0900header.h"
void selection_sort(vector<string> a[],int size)
{
int next;
for (next = 0; next < size - 1; next++)
{
print(a,size);//to see what is being passed to the function
int min_pos = min_position(a,next,size - 1);
swap(a[next],a[min_pos]);
}
}
int min_position(vector<string> a[],int to)
{
int min_pos = from;
for (int i = from + 1; i <= to; i++)
{
if (a[i] < a[min_pos])//read access violation happens here
{
min_pos = i;
}
}
return min_pos;
}
void print(vector<string> a[],int size)
{
for (int i = 0; i < size; i++)
{
cout << &a[i] << " ";//is printing memory locations instead of values
}
cout << endl;
}
inserttion_sort文件
#include "flore0900header.h"
void bubble_sort(vector<string> a[],int size)
{
for (int i = 0; i < size - 1; i++) //loop for recording no# of iteration needed to complete the sorting
{
int flagForSwap = 0; //creates a flag variable that accounts for wheather the swap function is called at all
//loop for counting comparisons
for (int j = 0; j < size - 1 - i; j++)
{
if (a[j] > a[j + 1]) //(read access violation happens here) compare adjacent array elements
{
swap(a[j],a[j + 1]); //completes the swap
flagForSwap = 1; // flag to 1 if swap is used
}
}
if (flagForSwap == 0) //breaks the iteration loop if inputed array is already sorted and no swap is needed
{
break;
}
}
}
void swap(int& x,int& y)
{
int temp = x; //creates a temp variable to store the value of the current element
x = y; // change the value of the current element to next element
y = temp; //assigns the value of temp to next element
}
merge_sort文件
#include "flore0900header.h"
void insertion_sort(vector<string> a[],int size)
{
for (int i = 1; i < size; i++)
{
vector<string> next = a[i];//read access violation happens here
int j = i;
while (j > 0 && a[j - 1] > next)
{
a[j] = a[j - 1];
j--;
}
a[j] = next;
}
}
quick_sort文件
#include "flore0900header.h"
void merge_sort(vector<string> a[],int to)
{
if (from == to)
{
return;
}
int mid = (from + to) / 2;
merge_sort(a,mid);
merge_sort(a,mid + 1,to);
merge(a,mid,to);
}
void merge(vector<string> a[],int to)
{
int n = to - from + 1;
vector<string>* b = new vector<string>[n];
int i1 = from;
int i2 = mid + 1;
int j = 0;
while (i1 <= mid && i2 <= to)
{
if (a[i1] < a[i2])//read access violation happens here
{
b[j] = a[i1];
i1++;
}
else
{
b[j] = a[i2];
i2++;
}
j++;
}
while (i1 <= mid)
{
b[j] = a[i1];
i1++;
j++;
}
while (i2 <= to)
{
b[j] = a[i2];
i2++;
j++;
}
for (j = 0; j < n; j++)
{
a[from + j] = b[j];
}
delete[] b;
}
解决方法
没有“向量数组”之类的东西。
您可以具有向量数组,但您没有。
但是,在这里,您的函数编写起来就像是这样:
int partition(vector<string> a[],int from,int to)
// ^^
我想这样做是因为,在您在此处添加[]
和在此处添加&
之前,您的函数似乎没有任何作用。
那是因为您要通过值传递向量 ,因此对副本进行了函数更改,因此未反映在调用范围中。
您的更改使代码得以编译,甚至在某些晦涩的情况下甚至可以“工作”,但这只是偶然的; [INDEX]
语法在向量和数组之间共享。但是您没有数组,因此假装执行的功能是错误的。您的大多数访问都超出了范围。
还请注意,如果您未在某处定义bool operator<(const vector<string>&,const vector<string>&)
,则它不会进行编译。
无论如何,使用一个向量的解决方案很简单:
- 摆脱那个
[]
- 摆脱那个
&
- 将现在的
vector<string>
更改为vector<string>&
。&
的意思是“参考”。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。