为什么std :: begin()和std :: end()适用于数组而不是指针[几乎是数组]和数组的引用[它是原始数组的别名].
在我挠头15分钟后,我无法在谷歌中得到任何东西.
下面只有第一个案例,而不是第二个和第三个,这可能是什么原因?
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
int main()
{
int first[] = { 5, 10, 15 }; // Fist Case
if (std::find(std::begin(first), std::end(first), 5) != std::end(first)) {
std::cout << "found a 5 in array a!\n";
}
int *second = new int[3]; // Second Case
second[0] = 5;
second[1] = 10;
second[2] = 15;
if (std::find(std::begin(second), std::end(second), 5) != std::end(second)) {
std::cout << "found a 5 in array …Run Code Online (Sandbox Code Playgroud) 我想更新矢量'v',以便我可以从0-100开始迭代.
我知道这是不允许的,但如果我只想这样做怎么办?有什么办法吗?
int main() {
// your code goes here
vector<int> v;
v.push_back(1);
int count = 0;
for(int elem: v){
if(count<100)
v.push_back(count);
count++;
}
for(int elem: v)
cout << elem << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是:
1
0
Run Code Online (Sandbox Code Playgroud) 我只是初学者在c ++ 11中的移动操作,所以玩它.但发现了一些我无法理解的东西.
#include <iostream>
using namespace std;
class A{
public:
A(){cout << "default ctor" << endl;}
A(const string& str):_str{str}{cout << "parameter ctor" << endl;}
A(A&& obj):_str{std::move(obj._str)}{cout << "move ctor" << endl;}
A& operator =(A&& rhs){_str = std::move(rhs._str);cout << "move assignment operation" << endl; return *this;}
void print(){cout << _str << endl;}
private:
string _str;
};
int main(){
A a("rupesh yadav"); // parameter ctor
A b(std::move(a)); // move ctor
cout << "print a: ";
a.print(); // NOT printing --> CORRECT!! …Run Code Online (Sandbox Code Playgroud) 我的问题是如何stringstream在wstring或wstringstream
stringstream fileSearch;
fileSearch<<fileOutput.str();
fileSearch<<"*.jpg";
cout<<fileSearch.str()<<endl;
Run Code Online (Sandbox Code Playgroud)
这是我的代码。我想将这个fileSearch字符串流转换成wstring ...有人可以帮我请c ++示例代码...我想在
int numOfFiles(wstring searchPath);
Run Code Online (Sandbox Code Playgroud)
这个功能...
我想要一个函数,它将返回指针的值,无论它指向什么级别的指针.喜欢它可能是单个或双指针或三指针或更多,但该函数应返回该值.
例:
#include <iostream>
using namespace std;
template <class T>
T func(T arg){
// what to do here or there is some other way to do this?????
}
int main() {
int *p, **pp, ***ppp;
p = new int(5);
pp = &p;
ppp = &pp;
cout << func(p); // should print 5
cout << func(pp); // should print 5
cout << func(ppp); // should print 5
return 0;
}
Run Code Online (Sandbox Code Playgroud)
所以,现在我想在一个函数中传递这个p,pp,ppp,它应该打印或返回值'5'.
如果源文件是source.cpp,那么编译器输出应该在我的目录中有source.i source.s source.o,而不仅仅是.o文件.
哪里
- preprocessed = source.i
- assembly = source.s
- object = source.o
我知道前两个文件正在创建但后来他们只删除.o文件显示,以便链接器可以链接目标文件,但我也希望看到这两个文件.
对于linux任何标志或什么?
我找到了这段代码,并想知道我是否真的应该在我的真实项目中实现这样的东西.
令我困惑的事情是
这需要更多的编译时间,但我不应该以运行时为代价来打扰编译时间.
如果N真的是一个非常大的数字呢?源代码中是否有文件大小限制?
或者它的好事要知道,而不是实施?
#include <iostream>
using namespace std;
template<int N>
class Factorial {
public:
static const int value = N * Factorial<N-1>::value;
};
template <>
class Factorial<1> {
public:
static const int value = 1;
};
int main() {
Factorial<5L> f;
cout << "5! = " << f.value << endl;
}
Run Code Online (Sandbox Code Playgroud)
输出:
5! = 120
Run Code Online (Sandbox Code Playgroud)
稍微修改一下,因为我正在玩代码,发现了
Factorial<12> f1; // works
Factorial<13> f2; // doesn't work
Run Code Online (Sandbox Code Playgroud)
错误:
undefined reference to `Factorial<13>::value'
Run Code Online (Sandbox Code Playgroud)
是不是可以12进一步深入?