我期待找到数组中的最低正值及其在列表中的位置.如果列表中的值重复,则只关注FIRST实例.这就是我所拥有的,它包括我想要的但包括0.
print "Position:", myArray.index(min(myArray))
print "Value:", min(myArray)
Run Code Online (Sandbox Code Playgroud)
例如,如果,
myArray = [4, 8, 0, 1, 5]
然后位置:2,值:0
我希望它呈现位置:3,价值:1
使用:MSVS2012
码
elemalg.h
#include <vector>
#include <string>
#include <fstream>
class ElemAlg
{
private:
std::string difficultlyLevel, question, answerToRead;
std::vector<std::string> questions, answers;
std::vector<std::string> GetQuiz(int);
};
Run Code Online (Sandbox Code Playgroud)
elemalg.cpp
#include "elemalg.h"
std::vector<std::string> ElemAlg::GetQuiz(int difficulty)
{
if (difficulty == 1) { difficultyLevel = "algE"; }
if (difficulty == 2) { difficultyLevel = "algM"; }
if (difficulty == 3) { difficultyLevel = "algH"; }
if (difficulty == 4) { difficultyLevel = "algVH"; }
std::ifstream fin(difficultyLevel + ".txt");
while (std::getline(fin, question)) { questions.push_back(question); }
fin.close();
std::ifstream fin2(difficultyLevel + …
Run Code Online (Sandbox Code Playgroud) 这是我目前的职能
long long int File::Getline3(int user1, long long int user3)
{
std::string filename = std::to_string(user1);
std::ifstream fin(filename + ".txt");
fin.getline (line1, 5);
fin.getline (line2, 5);
fin.getline (line3, 20);
fin.close();
user3 = (atoi(line3));
return user3;
}
Run Code Online (Sandbox Code Playgroud)
似乎返回大部分数字,但不是全部,并以混乱的格式.
不确定我的错误在于读取行(getline)还是转换它(atoi)或返回它.
(在头文件中声明的line1,line2和line3 char)
第3行是16位数.
所以我是Perl的新手,我发现我想编写一个倒计时代码,并且真的很挣扎,我在http://www.perlmonks.org/bare/?node_id=407922找到了一些代码并试图弄清楚会发生什么但部分失败了.
麻烦是我讨厌使用我不理解的代码,所以希望有人能够帮助我解决它的一些部分(下面的代码来自URL)
use 5.16.3;
use strict;
my $countdown = 1*60*60; # in seconds
$| = 1;
my $beginTime = time;
my $endTime = $beginTime + $countdown;
for (;;)
{
my $time = time;
last if ($time >= $endTime);
printf("\r%02d:%02d:%02d",
($endTime - $time) / (60*60),
($endTime - $time) / ( 60) % 60,
($endTime - $time) % 60,
);
sleep(1);
}
Run Code Online (Sandbox Code Playgroud)
下面我编号并复制了我不理解的代码行
1:1*60*60
我理解60*60,但为什么1?
2:$| = 1;
对此没有任何线索
3:for(;;)
不确定在;;
做什么?
4:不知道这是做什么的?(数学上或语法上).
printf("\r%02d:%02d:%02d",
($endTime - …
Run Code Online (Sandbox Code Playgroud) 我想从文本文件中读取第三行作为 a string
,将其转换为 a long long int
,然后返回该值。
文本文件第三行的数据是 1234567890123456
long long int File::Getline3(int user1, int user3)
{
std::string filename = std::to_string(user1);
std::ifstream fin(filename + ".txt");
fin.getline (line1, 5);
fin.getline (line2, 5);
fin.getline (line3, 20);
fin.close();
// convert line 3 to a string called str
const char *line3;
std::string str(line3);
// convert str to long long int called user3
long long int strtoll(const char *nptr, char **endptr, int base);
char* endptr = NULL;
user3 = strtoll(str.c_str(), &endptr, 10);
return …
Run Code Online (Sandbox Code Playgroud) 我正在编写一个函数,让用户有机会更改他们的用户名。为此,我尝试重命名存储数据的文件。我想出了一种方法来做到这一点,虽然不太有效,但我认为我已经很接近了。
文件名最初是这样创建的 -
std::cout << "Please enter a username: ";
std::getline (std::cin, username);
std::ofstream fout (username + ".txt");
Run Code Online (Sandbox Code Playgroud)
效果很好。如果他们后来选择更改用户名
std::cout << "Please type in your new username." << std::endl;
std::getline (std::cin, newUsername);
std::ofstream fout (newUsername + ".txt");
// I copied the contents of username.txt to newUsername.txt here
Run Code Online (Sandbox Code Playgroud)
这又工作得很好。
问题就出在下面。
问题在于删除原始文件,更具体地说是添加文件扩展名 .txt 时
我已经为remove() 功能添加了#include 并将.c_str() 添加到用户名中,因为我相信remover () 只会采用C 字符串(char*),而不是C++ 字符串。
remove(username.c_str() + ".txt"); // error expression must have integral or enum type referring to ".txt".
Run Code Online (Sandbox Code Playgroud)
预先感谢大家的时间
我简要地浏览了各种c ++网站和教科书.但他们都没有任何与我所寻找的相关的东西.
我想要的是c ++中的列表,其中可以包含int,string和int数组变量.但在我花几个小时玩一些代码之前,我想知道是否有人知道这样的事情是否确实存在?我不是要求代码显示给我,如果有可能,我会尝试,然后询问我遇到的任何问题.
谢谢
所以我有一个文本文件,内容如下:
1, 2, 3, 4, 5
2, 3, 4, 5, 6
3, 4, 5, 6, 7
4, 5, 6, 7, 8
...
Run Code Online (Sandbox Code Playgroud)
我想将此文件读入python中的2D数组中,以便该matrix[]
数组将保存包含每行整数的数组。
例如, matrix[2][3] == 6
我不确定我的逻辑或编程是否正确(我是python的新手),但这是到目前为止的内容:
matrix = []
i = 0
with open('matrix.txt', 'r') as openfile:
for line in openfile:
matrix.append([])
matrix[i] = int(n) for n in line.split(',')
i += 1
openfile.close()
print (matrix)
#print (matrix[2][3])
Run Code Online (Sandbox Code Playgroud)
我收到覆盖嵌套for
语句的无效语法错误,如下所示int (n) for n