C++中的a std::vector
和a有什么区别std::array
?什么时候应该优先于另一个?各自的优点和缺点是什么?我的所有教科书都列出了它们是如何相同的.
我已经看到新线路\n
在我一直在研究的一些代码示例中使用了两种不同的方式.第一个是'\n'
第二个,第二个是"\n"
.有什么区别,为什么要使用'\n'
?
我理解'\n'
代表一个字符并"\n"
代表一个字符串,但这有关系吗?
函数定义是在int(main)之前还是在int main之后是否重要?
我已经看到了两种方式,并试图找到显示函数定义和声明的正确方法.
处理多维数组时,是否可以为数组分配两种不同的变量类型...
例如已在阵列int example[i][j]
是有可能i
和j
是两个完全不同的变量类型,如int和字符串?
将某些内容保存到文本文件时,是否有办法不覆盖文件中的内容?
EX.
blah.txt
这是一个例子
fout << "of saving to a file.";
Run Code Online (Sandbox Code Playgroud)
我希望将"保存到文件"添加到"这是一个示例"而不是覆盖它.
到目前为止,我所有的定义都是定义.
double power(double base, int exponent);
double factorial(double n);
Run Code Online (Sandbox Code Playgroud)
在此之后,我完全失去了,我正在寻找想法.
我正在尝试将一个数组排序为最小到最大,我真的迷失了......
这是我到目前为止:
int temp, temp2;
for (int x = 0; x < array_size; x++)
{
temp=a[x];
for (int i = 0; i < array_size; i++)
{
if (a[i] < temp)
{
temp2=a[i];
a[i]=temp;
a[x]=temp2;
}
}
}
Run Code Online (Sandbox Code Playgroud)
更新:仍然无法正常工作,我必须使用代码.
int temp, temp2, x=-1;
for (int x = 0; x < array_size; x++)
{
temp=a[x];
for (int i = x+1; i < array_size; i++)
{
if (a[i] < temp)
{
temp2=a[i];
a[i]=temp;
a[x]=temp2;
}
}
}
Run Code Online (Sandbox Code Playgroud) 当我运行这个程序时得到两个警告,我无法弄清楚如何阻止这种情况发生.任何帮助,将不胜感激!
警告:有符号和无符号整数表达式之间的比较
是我在extract_word函数中获得2行的警告.
#include<iostream>
#include<string>
using namespace std;
class StringModify {
public:
void get_data();
//takes a line of input from the user
string& extract_word(string& a);
//extracts each word from the input line
string& reverse_word(string& s);
//returns a string which is reverse of s
void rev();
void swap(char& v1, char& v2);
//interchanges value of v1 and v2
void append(const string& reverse_word);
//puts together each reversed word with whitespaces to get formatted_line
void display();
//diss both input line and formatted_line
private:
string …
Run Code Online (Sandbox Code Playgroud)