我想用C++打印出一个向量的内容,这就是我所拥有的:
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <vector>
#include <sstream>
#include <cstdio>
using namespace std;
int main()
{
ifstream file("maze.txt");
if (file) {
vector<char> vec(istreambuf_iterator<char>(file), (istreambuf_iterator<char>()));
vector<char> path;
int x = 17;
char entrance = vec.at(16);
char firstsquare = vec.at(x);
if (entrance == 'S') {
path.push_back(entrance);
}
for (x = 17; isalpha(firstsquare); x++) {
path.push_back(firstsquare);
}
for (int i = 0; i < path.size(); i++) {
cout << path[i] << " ";
}
cout << endl;
return …Run Code Online (Sandbox Code Playgroud) 对于我的C++任务,我基本上是尝试vec从左边第二个顶部字符开始搜索文本文件中的一大块文本(流式传输到我的向量).这是一个文本迷宫,我的程序最终应该打印出通过它的路径的字符.
迷宫的一个例子是:
###############
Sbcde####efebyj
####hijk#m#####
#######lmi#####
###############
###############
###############
###############
###############
###############
###############
###############
###############
###############
###############
Run Code Online (Sandbox Code Playgroud)
'#'是一个不可行走的墙,你总是从第二个顶部角色的左边开始.字母字符代表可步行的方格.出口总是在右边.迷宫在maze.text文件中始终是15x15大小.按字母顺序排列的字符在同一个迷宫中重复,但不能直接在彼此旁边.
我在这里要做的是:如果当前正方形旁边的方格有一个字母字符,请将其添加到向量中vec,然后重复此过程,直到我到达迷宫的末尾.最终我应该通过在屏幕上打印一些迷宫中存在的多条路径来使这更复杂.
到目前为止,我有这个算法本身,我知道这是错误的:
void pathcheck()
{
if (isalpha(vec.at(x)) && !(find(visited.begin(), visited.end(), (vec.at(x))) != visited.end()) )
{
path.push_back(vec.at(x));
visited.push_back(vec.at(x));
pathcheck(vec.at(x++));
pathcheck(vec.at(x--));
pathcheck(vec.at(x + 16));
pathcheck(vec.at(x - 16));
}
}
Run Code Online (Sandbox Code Playgroud)
visited 是我的矢量跟踪访问过的广场.
我如何更新它,以便它实际工作,最终我可以管理多个路径(即如果有2个路径,程序将打印到屏幕上的两个路径)?我记得被告知我可能需要另一个矢量/数组来跟踪我已经访问/检查过的方块,但是我如何在这里实现呢?
我的作业问题:
已经宣布了一系列名为parkingTickets的整数,并将其初始化为自当年年初以来城市警察每天发出的停车票数量.(因此,数组的第一个元素包含1月1日给出的票数;最后一个元素包含今天给出的票数.)
已声明并初始化名为ndays的变量以保存数组的大小.(因此,如果今天是1月18日,则ndays将具有值18;如果今天是2月3日,则ndays将具有值34.)
此外,已经声明了名为mostTickets的变量以及变量k.
如果不使用任何其他变量,并且不更改ndays的值或parkingTickets数组的元素,请编写一些代码,这些代码会导致mostTickets包含parkingTickets中找到的最大值.
为此,我有以下代码:
for(k = 0; k < ndays; k++) {
if (parkingTickets[k] > parkingTickets[ndays]) {
mostTickets = parkingTickets[k];
}
}
Run Code Online (Sandbox Code Playgroud)
但我的运动提交者说这是错的.我的代码出了什么问题?我也试过parkingTickets[ndays - 1]了,但那也行不通.
for( k = 0; k < n; k++ )
{
total += total + temps[k];
}
avgTemp = total / n;
Run Code Online (Sandbox Code Playgroud)
temps是我的数组,包含n元素.avgTemp存储所有值的平均值temps.k只是一些整数使我的循环工作.k,n和total已经在上面适当地声明了.total跟踪数组中元素的总数.
我的运动用品告诉我这是错的.我究竟做错了什么?
对于链接:
有人可以向我解释这个概念并为我提供一个理论示例和一个简单的代码吗?
我的想法是“每个表位置都指向散列到该位置的项目的链接列表(链)”,但我似乎无法说明实际发生的情况。
假设我们有 h(x)(哈希函数)= x/10 mod 5。现在哈希 12540, 51288, 90100, 41233, 54991, 45329, 14236,那会是什么样子?
对于开放寻址(线性探测、二次探测和每个 R 位置的探测),有人也可以向我解释一下吗?我尝试用谷歌搜索,但我似乎更加困惑了。
我想将文本文件中的一些文本放入数组中,但将数组中的文本作为单个字符.我该怎么办?
目前我有
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <vector>
#include <sstream>
using namespace std;
int main()
{
string line;
ifstream myfile ("maze.txt");
if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile,line);
// --------------------------------------
string s(line);
istringstream iss(s);
do
{
string sub;
iss >> sub;
cout << "Substring: " << sub << endl;
} while (iss);
// ---------------------------------------------
}
myfile.close();
}
else cout << "Unable to open file";
system ("pause");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我猜getline一次得到一行.现在,我将如何将该行拆分为单个字符,然后将这些字符放入数组中?我是第一次参加C++课程,所以我是新人,很高兴:p
我希望每当你运行C++程序时弹出控制台窗口......但是在我的代码中没有发生这种情况.它很快消失了.怎么了?注意:我是C++的新手.
出于某种原因,当我仅使用main()函数来保存所有内容而没有第二个函数时,它可以正常工作,但出于我的任务目的,我无法将所有内容都填入main().
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <cstdio>
using namespace std;
ifstream file("maze.txt");
vector<char> vec(istreambuf_iterator<char>(file), (istreambuf_iterator<char>())); // Imports characters from file
vector<char> path; // Declares path as the vector storing the characters from the file
int x = 18; // Declaring x as 18 so I can use it with recursion below
char entrance = vec.at(16); // 'S', the entrance to the maze
char firstsquare = vec.at(17); // For the …Run Code Online (Sandbox Code Playgroud)