今天我试图在没有二进制搜索代码的情况下在向量中找到一个元素的位置,我发现有 find() stl 这样的东西我实现了它并且它正在编译但它没有给出正确的输出
这是我的代码:
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
vector<int> a(0);
a.push_back(6);
a.push_back(3);
a.push_back(9);
a.push_back(5);
a.push_back(1);
vector<int> :: iterator b;
b = find(a.begin() , a.end() , 3);
cout << (a.end() - b); // output is 4
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我应该怎么做才能获得向量中任何元素的位置?谢谢
我正在阅读有关循环排序算法的内容,发现它本质上是不稳定的,但是,我很难想出一个案例来显示循环算法的不稳定性质。有人可以给出一个案例,我们可以观察到算法的不稳定性质吗?
有关该算法的更多信息:- https://en.wikipedia.org/wiki/Cycle_sort
这是我的循环排序算法代码:-
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int *arr;
arr=new int[n];
for (int i = 0; i < n; i++)
cin >> arr[i];
int cyStart, item, pos;
for (int cyStart = 0; cyStart < (n - 1); cyStart++)
{
item = arr[cyStart];
pos = cyStart;
for (int i = cyStart + 1; i < n; i++)
{
if (item > arr[i])
pos++;
}
if (pos == cyStart)
continue;
while (item …Run Code Online (Sandbox Code Playgroud) #include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main() {
unordered_map<string,set<int>> map;
set<int> s;
s.insert(1);
s.insert(2);s.insert(3);
map.insert(make_pair("Screen1",s));
for(auto it : map)
{
cout<<it.first<<endl;
it.second.insert(5);
}
for (auto i : map["Screen1"])
{
cout<<i<<endl;
}
}
Run Code Online (Sandbox Code Playgroud)
在上述代码中,我试图在地图内的集合中插入一个值 5。但 it.second.insert(5); 不成功
这是我得到的输出
Screen1
1
2
3
Run Code Online (Sandbox Code Playgroud) #include <bits/stdc++.h>
using namespace std;
#include <unordered_set>
#include <queue>
struct word {
string s;
int level;
word(string a, int b)
: s(a)
, level(b)
{
}
};
bool isadj(string s1, string s2)
{
int len = s1.length(), count = 0;
for (int i = 0; i < len; i++) {
if (s1[i] != s2[i])
count++;
if (count > 1)
return false;
}
return count == 1 ? true : false;
}
int ladderLength(string beginWord, string endWord, vector<string>& wordList)
{
unordered_set<string> …Run Code Online (Sandbox Code Playgroud) 据说 anunordered_map<int,int>比 a 占用更多的空间vector<int>。虽然我完全意识到这一点,但我想知道如何在C++ 中获得 an 的单个实例的大致大小。现在,假设我向其中插入了元素。我认为占用的内存乘以某种常数,但是,我无法在 Internet 上的任何地方找到准确的答案。 这就是我正在做的事情。我想计算使用了多少内存,而无需编写任何代码。有没有办法做到这一点?unordered_mapn = 1000000nu_m
#include<bits/stdc++.h>
using namespace std;
const int N = 1000000;
unordered_map<int,int> u_m ;
int main(){
for(int i = 0;i<N;i++){
u_m[i] = 123+i;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果有差别,我故意把u_m外面的main
我创建了一个具有一些基本属性的 Animal 类,并添加了一个无数据构造函数。我还重载了 ostream 运算符来打印属性。
动物.cpp
#include<bits/stdc++.h>
using namespace std;
class Animal {
string name;
int action;
public:
Animal() {
name = "dog";
action = 1;
}
ostream& write(ostream& os) {
os << name << "\n" << action << "\n";
return os;
}
friend ostream& operator<<(ostream& os, Animal &animal) {
return animal.write(os);
}
};
int main() {
cout << "Animal: " << Animal() << "\n";
}
Run Code Online (Sandbox Code Playgroud)
但是我在主要错误中发现二进制表达式 ostream 和 Animal 的操作数无效。如果我声明 Animal 然后调用 cout ,效果很好。但是如何让它像这样工作(同时初始化和cout)?
考虑这段代码
#include <bits/stdc++.h>
using namespace std;
struct foo {
template <typename T>
foo& operator<< (const T& var) {
cout << var;
return *this;
}
} bar;
int main() {
bar << 1 << '\n';
bar << 1.2 << '\n';
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想<<只为整数数据类型重载运算符.(int16_t,int32_t,int64_t)
我怎样才能做到这一点?
最近我有c_str()的问题.下面是示例代码片段
#include<bits/stdc++.h>
#include<unistd.h>
using namespace std;
class har{
public:
string h;
har(string str){
h=str;
}
};
int main(){
har *hg=new har("harish");
const char *ptr=hg->h.c_str();
delete hg;
cout<<ptr<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到输出"harish"....我已经销毁了对象,但我仍然得到输出..是c_str()再次在堆中分配内存.
我想检查地图上是否存在钥匙(本身就是一对)。
我是新来使用map的人,无法找到要检查键(一对)的函数。
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
typedef pair<ll,ll> my_key_type;
typedef map<my_key_type,ll> my_map_type;
int main()
{
my_map_type ma;
my_map_type::iterator it;
ma.insert(make_pair(my_key_type(30,40),6));
it=ma.find(30,40);
if(it==ma.end())
{
cout<<"not present";
return 0;
}
cout<<"present";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误-
no matching function for call to ‘std::map<std::pair<long long int, long long int>, long long int>::find(int, int)’ it=ma.find(30,40);
Run Code Online (Sandbox Code Playgroud) 我没有得到 elab 问题的正确输出。这是问题:
系里有院系,你必须根据身份证号来安排院系的名单。
测试案例 1
输入
Run Code Online (Sandbox Code Playgroud)5 Ram 101 Rahul 95 Ashwin 75 Ahamed 106 Saurav 110输出
Run Code Online (Sandbox Code Playgroud)After Sorting Name ID Ashwin 75 Rahul 95 Ram 101 Ahamed 106 Saurav 110
这是我使用结构实现的代码:
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
struct faculty{
string name;
int id;
};
int main(){
int n;
struct faculty arr[n];
for(int i=0;i<n;i++){
cin>>arr[i].name;
cin>>arr[i].id;
}
cout<<"After Sorting"<<endl;
cout<<"Name ID"<<endl;
//insertion sort
for(int i=1;i<n;i++){
struct faculty key=arr[i];
int j=i-1;
while(j>=0 && arr[j].id>key.id)
{
arr[j+1]=arr[j];
j--;
}
arr[j+1]=key;
} …Run Code Online (Sandbox Code Playgroud)