这是我的代码:
std::cout << "The contaner is " << (!container)?"not":; "empty";
Run Code Online (Sandbox Code Playgroud)
这显然不起作用,但我希望现在这个想法很清楚。我想打印"The container is empty",并"not"在"empty"if bool containeris之前添加false。
我想知道是否有可能,或者我是否必须按照以下方式写一些东西:
if(container) std::cout ...;
else std::cout ...;
Run Code Online (Sandbox Code Playgroud) 我有一个包含文本的列列表,但我只是从这个列表中获取第一行。怎么做?
#include <stdio.h>
int main()
{
FILE *fr;
char c;
fr = fopen("prog.txt", "r");
while( c != EOF)
{
c = fgetc(fr); /* read from file*/
printf("%c",c); /* display on screen*/
}
fclose(fr);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有一组地图:
std::array<std::map<double, double>, 8> freqMap;
Run Code Online (Sandbox Code Playgroud)
填充它时,我需要在不同的数组索引处向地图添加条目。我知道我可以创建 8 个不同的地图,填充它们,然后将它们添加到数组中,但是是否可以直接在数组中将条目附加到地图?
例如,我如何将着手添加的映射条目key5.0,val3.3至数组索引2,然后添加另一条目数组索引3,然后附加另一条目索引2再次等。
我也可以使用std::vector地图,但仍然看不到以这种方式添加条目的方法。
这是一个例子。我正在从文件中读取数据并想直接更新我的数据结构:
while (fin >> arrayIdx >> key>> val)
freqMaps[arrayIdx] = ??
Run Code Online (Sandbox Code Playgroud) 我正在尝试将一个结构写入.dat文件,当我打开它时,它会向我显示以下内容:
“1^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@041720881^@^@^@^@^@^ @^@^@^@^@^@Denko^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@Brenko^@^@^@^ @^@^@^@^@^@^@^@^@^@^@13.07.2000^@^@^@^@^@^@^@^@^@^@ "
它在实际值之间添加随机符号。现在,当我至少尝试读取和打印一些值时,它就是不起作用。就像缓冲区是空的一样。但我遵循了我阅读的说明和指南。
使用fwrite或类似的方法不是一个选择,因为我必须使用这些特定的函数write()和read().
我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
struct info{
char id[20];
char telefon[20];
char ime[20];
char priimek[20];
char datum[20];
};
Run Code Online (Sandbox Code Playgroud)
int main(int argc, char* argv[]) {
struct info dude =
{
"01",
"041720881",
"Denko",
"Brenko",
"13.07.2000"
};
struct info dude2 =
{
"02",
"041581734",
"Denko",
"Badenko",
"13.07.1990"
};
if(strcmp(argv[1], "-c") == 0){
int fd = open("xpo.dat", O_CREAT| O_APPEND | S_IRWXG, 0666); …Run Code Online (Sandbox Code Playgroud) 我进入out_of_range我的代码。我必须如何解决这个问题?有2个功能。第一个函数检查字符串是否为回文。第二个函数必须从向量中找到回文并将其复制到作为返回值的新向量。
#include "pch.h"
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
bool IsPalindrom(string a)
{
string b = a;
reverse(a.begin(), a.end());
if (b == a)
{
cout << "success " << endl;
return true;
}
else {
cout << "error";
return false;
}
}
vector<string> PalindromFilter(vector<string> words, int minLength)
{
vector<string> pol;
for (int i = 0; i <= words.size(); ++i)
{
if (IsPalindrom(words[i]) && words[i].size() > minLength)
{
pol.at(i) = words.at(i);
}
}
return …Run Code Online (Sandbox Code Playgroud) 在下面的代码中,为什么除非我放入term = 1.0/n而不是 when ,否则我不会得到有效的结果term = 1/n。我已将 term 声明为浮动,这还不够吗?
#include <stdio.h>
int main()
{
float sum = 0, term;
int n, i;
printf("enter the value of n:\n");
scanf("%d", &n);
term = 1.0 / n;
for(i = 1; i <= n; i++)
{
sum = term + sum;
}
printf("Sum = %.3f\n", sum);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个函数,该函数将从用户那里获取输入并创建一个倒三角形。但是,我在函数的末尾(第 29 行)收到一个错误,指出“控制到达非空函数的末尾”。
我认为它与返回值有关,但我不确定。此外,当我int number在函数内部更改为int num我收到一条错误消息“重新定义 'num'”。
请帮忙。解释发生了什么错误以及我如何修复它。
谢谢你。
#include <iostream>
using namespace std;
int row(int num)
{
int number;
int decreasedNumber;
for(int i = number; i >= 0; i -= 2)
{
decreasedNumber = i;
//decreased number from the outer loop will decrease once again
for(int j = decreasedNumber; decreasedNumber >= 0; decreasedNumber -=2)
{
cout << decreasedNumber << " ";
}
cout << endl;
}
}
int main()
{
//Prompting the user to enter …Run Code Online (Sandbox Code Playgroud) 我无法在IF/ELSE嵌套在for循环中的语句中发现我的错误在哪里。我确实尝试使用while循环来使我的IF/ELSE语句工作,但它没有按预期工作。
甚至我的Console.Writeline陈述也无法准确反映价格和尺寸。
我的代码:
static void Main(string[] args)
{
string[] size = { "S", "M", "L", "X"};
decimal[] price = { 6.99M, 8.99M, 12.50M, 15.00M};
Console.WriteLine("Please enter a pizza size : ");
string pizzaSize = Console.ReadLine();
int i;
for (i = 0; i < size.Length; i++)
{
if (size[i] == pizzaSize)
{
Console.WriteLine("Your pizza size is " + pizzaSize + ". The price is " + price[i].ToString("C"));
} else
{
Console.WriteLine("Please …Run Code Online (Sandbox Code Playgroud) 向所有程序员问好,我无法理解一些东西
char a[]="hello"
char* b="salam"
第一个问题是为什么我们不能修改 2,例如b[0]='m',我知道 2 被存储为编译时常量但我不明白它是什么意思以及 2 的 quiddity 是什么?
第二个问题:
3.
char a[]="hello";
char* c=a;
c[0]='r';
Run Code Online (Sandbox Code Playgroud)
现在我们可以修改然后打印c,但是我们不能修改 2 !为什么?
我无法理解这些指针的概念,请向我解释
你能告诉我为什么这个程序的 OUTPUT 是ABBBCDE吗?
这是我的考试题。
#include <stdio.h>
int main (void)
{
int i;
for (i = 0; i < 2; i++)
{
printf ("A");
for (; i < 3; i++)
printf ("B");
printf ("C");
for (; i < 4; i++)
printf ("D");
printf ("E");
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) 这不编译:
vector<int[2]> v;
int p[2] = {1, 2};
v.push_back(p); //< compile error here
Run Code Online (Sandbox Code Playgroud)
什么是替代方案?我不想使用std::array.
std::vector自身的声明编译。这push_back是不编译的。