我在我的代码中发布了一个问题,其唯一的#include指令如下:
#include <bits/stdc++.h>
Run Code Online (Sandbox Code Playgroud)
我的老师告诉我这样做,但在评论部分,我被告知我不应该这样做.
为什么?
c++ portability c++-faq turbo-c++ implementation-defined-behavior
我知道如果我们在变量声明中关闭尖括号后不放空格,C++会抛出以下错误.
‘>>’ should be ‘> >’ within a nested template argument list
但是如果我#define在这段代码中使用的话,错误就不会出现.有人可以解释一下吗?
我认为#define只是一个宏扩展并且像find-replace一样工作,所以在这里声明变量的方式应该是相同的.
如果我用C++ 11编译它也不会发生此错误.
#include <bits/stdc++.h>
using namespace std;
#define vi vector<int>
int main(){
//Doesn't work, compile error
vector<vector<int>> v;
//Works
vector<vi> vv;
}
Run Code Online (Sandbox Code Playgroud) 我试图在我的c ++代码中包含头文件位/ stdc ++,但似乎编译器不支持它,有没有办法让它工作?
我使用OS X Yosemite 10.10.2和Xcode 6.1.1
让我们说我想使用hex()功能.我知道它在<ios>头文件中定义,我也知道它包含在<iostream>头文件中.不同之处在于<iostream>更多功能和其他我不需要的东西.
从性能角度来看,我是否应该关注包含/定义更少的功能,类等等?
我有一个价值向量.我想根据这些值得到一个排序的索引列表.
我有合理的工作,除非出现相同的值.当出现相同的值时,我希望指数保持有序.
例如,我有这个测试用例:
std::vector< size_t > idx;
std::vector< int > val;
for( int i = 0; i < 40; i++ )
{
idx.push_back( i );
val.push_back( i % 10 );
}
std::sort( idx.begin(), idx.end(), [&]( size_t a, size_t b )
{
return val[a] < val[b];
} );
Run Code Online (Sandbox Code Playgroud)
这会将索引数组排序为以下内容:
(0,10,30,20,1,31,21,11,2,22,12,32,3,13,23,33,4,14,24,34,5,15,25,35,6,16,26,36,7,17,27,37,8,28,18,38,9,29,19,39)
Run Code Online (Sandbox Code Playgroud)
但我希望数组按以下顺序排列:
(0,10,20,30,1,11,21,31,2,12,22,32,3,13,23,33,4,14,24,34,5,15,25,35,6,16,26,36,7,17,27,37,8,18,28,38,9,19,29,39)
Run Code Online (Sandbox Code Playgroud)
有没有一种简单的方法可以修改我的lambda以按照上一个指定的顺序获取这些值?
提前干杯!
最近我遇到了一个问题但在此之前我会告诉你什么是参考
考虑这个计划
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector<string> RS;
string word;
while(cin>>word)
RS.push_back(word);
}
Run Code Online (Sandbox Code Playgroud)
此代码将间隔字符串中的每个单词存储在向量中
但问题来了.....
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector<string> RS,FS;
string word;
while(cin>>word)
RS.push_back(word);
while(cin>>word)
FS.push_back(word);
}
Run Code Online (Sandbox Code Playgroud)
这里的动机是将第一行的字符串字存储在RS中,将第二行的字符串字存储在FS矢量中
但它不会在一行结束时停止并将所有单词存储在RS中并且FS保持为空.
请建议一种正确执行相同程序的方法,或者如果您知道更有效的方式,那么您不仅仅是欢迎
提前致谢
我有一个带有模板函数和一个特殊函数的类,如下所示:
#include <bits/stdc++.h>
using namespace std;
class A
{
public:
template <typename T>
void test(const T& t)
{
cout << "template " << t << endl;
}
void test(const std::string& s) {
cout << "test " << s << endl;
}
};
int main()
{
A a;
a.test("asdas");
}
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,有两个test函数:
template功能std::string。我想要的是:
test(1)-> 调用template函数test<int>(1)std::string str = "asd"; test(str);-> 调用特殊函数test(str)test("asd")-> 调用特殊函数test(std::string str = "asd")如何实现这一目标?
我在解决leetcode问题https://leetcode.com/problems/largest-number/时遇到以下错误。
terminate called after throwing an instance of 'std::length_error'
what(): basic_string::_M_create
Run Code Online (Sandbox Code Playgroud)
因此,我在本地运行了类似的代码,它给了我分段错误。(顺便说一句,当向量大小较小(例如 10)时,它可以毫无问题地运行。我已经阅读了解决方案,所以我知道有更好的方法来解决这个 leetcode 问题。我只是想知道这个分段错误的细微差别。
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<string> arr(100, "0");
sort(arr.begin(), arr.end(), [](string &x, string &y) {
string a = x;
string b = y;
a.append(y);
b.append(x);
int n = a.length();
for (int idx = 0; idx < n; ++idx) {
if (a[idx] != b[idx])
return a[idx] > b[idx];
}
return true;
});
}
Run Code Online (Sandbox Code Playgroud) 有什么区别~i和INT_MAX^i
两者给出相同的没有.在二进制,但当我们打印否.输出不同,如下面的代码所示
#include <bits/stdc++.h>
using namespace std;
void binary(int x)
{
int i=30;
while(i>=0)
{
if(x&(1<<i))
cout<<'1';
else
cout<<'0';
i--;
}
cout<<endl;
}
int main() {
int i=31;
int j=INT_MAX;
int k=j^i;
int g=~i;
binary(j);
binary(i);
binary(k);
binary(g);
cout<<k<<endl<<g;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我把输出作为
1111111111111111111111111111111
0000000000000000000000000011111
1111111111111111111111111100000
1111111111111111111111111100000
2147483616
-32
Run Code Online (Sandbox Code Playgroud)
为什么k和g不同?
c++ bit-manipulation bit-shift bitwise-operators bitwise-xor
c++ ×9
string ×2
bit-shift ×1
bitwise-xor ×1
c ×1
c++-faq ×1
c++11 ×1
cin ×1
class ×1
header-files ×1
implementation-defined-behavior ×1
macos ×1
portability ×1
sorting ×1
stl ×1
templates ×1
turbo-c++ ×1
vector ×1
xcode ×1