有一个向量vector<int>v
我想vector<int>temp用这个向量以相反的顺序添加另一个向量.
例如,
v = {1, 5, 7} and
temp = {11, 9, 8}
Run Code Online (Sandbox Code Playgroud)
我想以相反的顺序添加temp,即{8, 9, 11}向量v.
那样,v将是:v = {1, 5, 7, 8, 9, 11}
这是我如何做到的:
int a[] = {1, 5, 7};
vector<int>v(a,a+3);
int b[] = {11, 9, 8};
vector<int>temp(b,b+3);
for(int i=temp.size()-1;i>=0;i--)
v.push_back(temp[i]);
for(int i=0;i<v.size();i++)
cout<<v[i]<<" ";
cout<<"\n";
Run Code Online (Sandbox Code Playgroud)
STL或C++中是否有内置函数来执行此操作?或者我必须手动完成吗?
我想打印一些这样的数据:
John 22
Shakib 25
Ponting 28
Run Code Online (Sandbox Code Playgroud)
在这里,我需要在名称后打印一些尾随空格.
我这样做了:
char *name[]={"John", "Shakib", "Ponting"};
int age[]={22, 25, 28};
int n=3;
for(int i=0; i<n; i++)
{
printf("%s",name[i]);
int cnt=10; // I need trailing spaces upto 10th column
int len=strlen(name[i]);
cnt-=len;
while(cnt)
{
printf(" ");
cnt--;
}
printf("%d\n",age[i]);
}
Run Code Online (Sandbox Code Playgroud)
有没有其他方法可以在不使用循环的情况下打印尾随空格?
我正在尝试编写一个批处理文件脚本,例如在路径中找到名为“Mdata”的目录名称"C:\project"
如果该路径中不存在“Mdata”,则继续在该路径中的每个目录中搜索,直到找到“Mdata”。如果找到,我将在该目录“Mdata”中执行一组命令。
我正在尝试使用"For /D"命令:
FOR /D %variable IN (set) DO command [command-parameters]
Run Code Online (Sandbox Code Playgroud)
但我不太了解它以及它是如何工作的。
请你的帮助,谢谢。
我需要在数字前打印一些前导空格和零,以便输出如下:
00015
22
00111
8
126
Run Code Online (Sandbox Code Playgroud)
在这里,我需要leading spaces在数字even和leading zero时间打印odd
我是这样做的:
int i, digit, width=5, x=15;
if(x%2==0) // number even
{
digit=log10(x)+1; // number of digit in the number
for(i=digit ; i<width ; i++)
printf(" ");
printf("%d\n",x);
}
else // number odd
{
digit=log10(x)+1; // number of digit in the number
for(i=digit ; i<width ; i++)
printf("0");
printf("%d\n",x);
}
Run Code Online (Sandbox Code Playgroud)
有没有捷径可以做到这一点?
我试图找到算法来将表格/列表中的少量资金加起来等于或可能最接近(但不大于)ceratin 数。
让我通过例子来解释它。我有一个数字列表:
{ 1.23 ; 3.45 ; 20.11 ; 100.13 ; 200.08 }
我想得到的数字是 123.69
所以应该采取 { 3.45 ; 20.11 ; 100.13 } = 123.69
如果数量是122它不应该采取相同的,但{ 20.11 ; 100.13 ; 1.23 } = 121.47
有什么想法如何写这样的东西吗?
我试图在音频文件的末尾检测静音.
我在ffmpeg库上取得了一些进展.在这里,我使用silencedetect列出音频文件中的所有静音.
ffmpeg -i audio.wav -af silencedetect=n=-50dB:d=0.5 -f null - 2> /home/aliakber/log.txt
Run Code Online (Sandbox Code Playgroud)
这是命令的输出:
- 音频文件的前端和末端静音 -
[silencedetect @ 0x1043060] silence_start: 0.484979
[silencedetect @ 0x1043060] silence_end: 1.36898 | silence_duration: 0.884
[silencedetect @ 0x1043060] silence_start: 2.57298
[silencedetect @ 0x1043060] silence_end: 3.48098 | silence_duration: 0.908
[silencedetect @ 0x1043060] silence_start: 4.75698
size=N/A time=00:00:05.56 bitrate=N/A
Run Code Online (Sandbox Code Playgroud)
- 音频文件的前端和末端没有静音 -
[silencedetect @ 0x106fd60] silence_start: 0.353333
[silencedetect @ 0x106fd60] silence_end: 1.25867 | silence_duration: 0.905333
[silencedetect @ 0x106fd60] silence_start: 2.46533
[silencedetect @ 0x106fd60] …Run Code Online (Sandbox Code Playgroud) 我想将所有索引值设置-1为双数组.
这是我的代码:
double dp[505];
memset(dp,-1,sizeof(dp));
cout<<dp[0]<<"\n";
Run Code Online (Sandbox Code Playgroud)
但是nan当我试图打印它的价值时,它就显示出来了.
什么nan意思?可以memset()在双数组中使用吗?
为什么以下引发错误?
for(; 0 ;) System.out.println("guess"); // or
for(;false;) System.out.println("guess"); // or
for(; 1 ;) System.out.println("guess");
Run Code Online (Sandbox Code Playgroud)
但以下运行正常(无限):
for(;true;) System.out.println("guess");
Run Code Online (Sandbox Code Playgroud)
为什么它适用true但不适用false?
我需要在运行时动态地在数字之前打印一些前导0.
这是我做的:
#include <stdio.h>
int main()
{
int leading_zero, n;
scanf("%d %d",&leading_zero, &n);
for(int i=0; i<leading_zero; i++)
printf("0");
printf("%d\n",n);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
没有循环有没有办法做到这一点?
我通过互联网搜索,我发现了这样的东西 - > printf("%05d\n",n)
将打印static前导0的数字
有没有办法在运行时这样做?