#include <iostream>
template<typename... Args>
void print(Args const&... args)
{
(std::cout << ... << args);
}
int main()
{
std::cout << 1 << 2 << 3 << std::endl; // ok
print(1, 2, 3); // ok
print(1, 2, 3, std::endl); // error! How to make it work?
}
Run Code Online (Sandbox Code Playgroud)
看在线演示
如何将函数模板作为模板参数传递?
这个程序:
#include <iostream>
#include <cstdlib>
#include <string>
int main(int argc, const char *argv[])
{
using ::std::cerr;
using ::std::cout;
using ::std::endl;
if (argc < 2 || argc > 3) {
cerr << "Usage: " << argv[0] << " [<count>] <message>\n";
return 1;
}
unsigned long count = 10000;
if (argc > 2) {
char *endptr = 0;
count = ::std::strtoul(argv[1], &endptr, 10);
if ((argv[1][0] == '\0') || (*endptr != '\0')) {
cerr << "Usage: " << argv[0] << " [<count>] <message>\n"; …Run Code Online (Sandbox Code Playgroud) 刚刚开始学习c ++,我非常难以置信.它是一种令人惊叹的语言,但我在覆盖文件时遇到了一些麻烦
#include <iostream>
#include <fstream>
using namespace std;
int main( )
{
double payIncrease = 7.6;
double annual;
double annualIncrease;
double newAnnual;
double monthlyIncrease;
double newMonthly;
ifstream inStream;
ofstream outStream;
Run Code Online (Sandbox Code Playgroud)
//这是问题所在
inStream.open("annualSalary.txt" );
outStream.open("newAnnualSalary.txt");
Run Code Online (Sandbox Code Playgroud)
如果我将newAnnualSalary.txt更改为annualSalary.txt,我会得到一些非常奇怪的数字.有谁知道为什么?
inStream >> annual;
inStream.close();
double monthly = (annual/12);
annualIncrease = ((annual/100)*payIncrease);
monthlyIncrease = ((monthly/100)*payIncrease);
newMonthly = (monthly + monthlyIncrease);
newAnnual = (annual + annualIncrease);
outStream <<"annual salary was: "<< annual << "\n" ;
outStream <<"new annual salary is " << newAnnual << "\n "; …Run Code Online (Sandbox Code Playgroud) 可能重复:
C++:"std :: endl"vs"\n"
我想知道这两种打印换行方式之间是否有任何显着差异:
cout << endl; //approach1
cout << "\n"; //approach2
Run Code Online (Sandbox Code Playgroud)
有什么实际区别吗?
printf("%-8s - %s\n", c->name ? c->name : "", c->help);
Run Code Online (Sandbox Code Playgroud) 我正在尝试用c ++试图找出如何尽可能快地打印从0到n的数字.
起初我只是用循环打印所有数字:
for (int i = 0; i < n; i++)
{
std::cout << i << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
但是,我认为这会在输出的每一个数字之后刷新缓冲区,而且肯定需要一些时间,所以我试图先将所有数字打印到缓冲区(或实际上直到它已满,因为它似乎似乎自动刷新)然后立即冲洗它们.然而,似乎在刷新缓冲区之后打印\n就像std :: endl一样,所以我省略了它:
for (int i = 0; i < n; i++)
{
std::cout << i << ' ';
}
std::cout << std::endl;
Run Code Online (Sandbox Code Playgroud)
这似乎比第一个例子快10倍.但是我想知道如何将所有值存储在缓冲区中并一次刷新所有值,而不是每次充满时都让它冲洗,所以我有几个问题:
编辑:似乎我的结果偏向于滞后系统(智能手机的终端应用程序)......使用更快的系统,执行时间没有显着差异.
再会,
我编写了一个 Java 程序,它使用 Process 对象和 Runtime.exec() 函数调用启动多个 C++ 编写的程序。C++ 程序使用 cout 和 cin 作为它们的输入和输出。Java 程序从 C++ 程序的输入流和输出流中发送信息和读取信息。
然后我有一个字符串缓冲区,它通过将 C++ 程序的输入和输出附加到字符串缓冲区来构建程序的典型交互。问题是所有输入调用都被追加,然后所有输出调用都被发布。例如,StringBuffer 的实例可能是这样的......
2
3
Please enter two numbers to add. Your result is 5
Run Code Online (Sandbox Code Playgroud)
当程序在标准控制台上看起来像这样
Please enter two numbers to add. 2
3
Your result is 5
Run Code Online (Sandbox Code Playgroud)
问题是我得到了输入和输出的顺序,因为除非 C++ 程序调用 cout.flush() 函数,否则在给出输入之前不会写入输出。
有没有办法自动刷新缓冲区,这样 C++ 程序就不必担心调用 cout.flush()?类似于 C++ 程序是一个与命令控制台交互的独立程序,程序员并不总是需要 cout.flush(),命令控制台仍然在输入之前输出数据。
谢谢,
我正在尝试包装一个对象,并希望将其作为指向函数的指针传递。已知该对象可以强制转换为有限类型,而我想为这些类型提供强制转换运算符。例如:
class MyInt {
public:
MyInt() {}
MyInt(int val): int32_storage_(val), int64_storage_(val*2) {}
const int32_t* GetInt32Ptr() const { return &int32_storage_; }
const int64_t* GetInt64Ptr() const { return &int64_storage_; }
private:
int32_t int32_storage_ = 0;
int64_t int64_storage_ = 0;
};
int32_t int32_add(const int32_t* iptr_a, const int32_t* const iptr_b) {
return *iptr_a + *iptr_b;
}
int64_t int64_add(const int64_t* iptr_a, const int64_t* const iptr_b) {
return *iptr_a + *iptr_b;
}
int main() {
MyInt a(10);
MyInt b(20);
std::cout << "32bit a + b = …Run Code Online (Sandbox Code Playgroud) 我已经制作了一个程序来计算8个字符串"sharjeel"的排列.
#include <iostream>
#include <time.h>
char string[] = "sharjeel";
int len = 8;
int count = 0;
void swap(char& a, char& b){
char t = a;
a = b;
b = t;
}
void permute(int pos) {
if(pos==len-1){
std::cout << ++count << "\t" << string << std::endl;
return;
}
else {
for (int i = pos; i < len;i++)
{
swap(string[i], string[pos]);
permute(pos + 1);
swap(string[i], string[pos]);
}
}
}
int main(){
clock_t start = clock();
permute(0);
std::cout << "Permutations: …Run Code Online (Sandbox Code Playgroud) 我想模仿map()C ++中的Ruby 方法。我正在努力自动找出返回类型:
#include <vector>
#include <string>
#include <algorithm>
#include <iostream>
typedef std::string T2;
template<class T1,
// class T2, // gives "couldn't deduce template parameter 'T2'"
class UnaryPredicate>
std::vector<T2> map(std::vector<T1> in, UnaryPredicate pred)
{
std::vector<T2> res(in.size());
std::transform(in.begin(), in.end(), res.begin(), pred);
return res;
}
int main()
{
std::vector<int> v1({1,2,3});
auto v2(map(v1, [](auto el) { return "'"+std::to_string(el+1)+"'"; }));
std::cout << v2[0] << "," << v2[1] << "," << v2[2] << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
这样可以编译,但T2固定为string。如果使用其他T2定义,编译器会抱怨couldn't …
c++ lambda templates template-argument-deduction generic-lambda