我的目标是找到总和给定总和的所有可能组合.例如,如果阵列是2 59 3 43 5 9 8 62 10 4并且如果总数是12,那么可能的组合是
2 10
3 9
8 4
5 3 4
Run Code Online (Sandbox Code Playgroud)
这是我写的第一组代码.想知道可以在这方面做出的最佳改进.
int find_numbers_matching_sum(int *number_coll, int total)
{
int *search_till = lower_bound(number_coll,number_coll+TOTAL_SIZE, total);
int location = search_till - number_coll;
if (*search_till > total && location > 0 )
{
--location;
}
while ( location >= 0 )
{
find_totals(number_coll,total,location);
--location;
}
return 1;
}
int find_totals(int *number_coll, int total, int end_location)
{
int left_ones = total - number_coll[end_location];
int curloc = end_location; …Run Code Online (Sandbox Code Playgroud) 我有一系列结构; 该数组的大小为N.
我想从数组中删除重复项; 也就是说,进行就地更改,将数组转换为每个结构的单一外观.另外,我想知道新的大小M(简化数组中的最高索引).
结构包括原语,因此比较它们是微不足道的.
如何在C++中有效地完成这项工作?
我已经实现了以下运算符:
bool operator==(const A &rhs1, const A &rhs2)
{
return ( ( rhs1.x== rhs2.x ) &&
( rhs1.y == rhs2.y ) );
}
bool operator<(const A &rhs1, const A &rhs2)
{
if ( rhs1.x == rhs2.x )
return ( rhs1.y < rhs2.y );
return ( rhs1.x < rhs2.x );
}
Run Code Online (Sandbox Code Playgroud)
但是,运行时出错:
std::sort(array, array+ numTotalAvailable);
* array will have all elements here valid.
std::unique_copy(
array,
array+ numTotalAvailable,
back_inserter(uniqueElements));
* uniqueElements will have non-valid elements.
Run Code Online (Sandbox Code Playgroud)
这有什么不对?
我觉得好像我正确使用它,但编译器感觉不然.我正在尝试使用stl排序算法在sort_by_name函数中按字母顺序对课程列表进行排序.这大致是我写的:
class SomeClass {
private:
struct course {
string id, name;
};
vector<course> COURSES;
bool nameCmp(course a, course b) {return (a.name > b.name) ? true : false;}
public:
void sort_by_name() {
sort(COURSES.begin(), COURSES.end(), nameCmp);
}
};
Run Code Online (Sandbox Code Playgroud)
错误:
error: no matching function for call to ‘sort(std::vector<SomeClass::course>::iterator, std::vector<SomeClass::course>::iterator, <unresolved overloaded function type>)’
Run Code Online (Sandbox Code Playgroud)
在此先感谢您的帮助.
我正在编写一个程序,该程序将读取具有社会安全号码的名称列表(当然不是真实的名称),并根据命令行参数根据姓氏或ssn对列表进行排序。为了简单起见,我已经重载了<运算符和重载了输入和输出运算符。一切都可以正常编译,直到我在main末尾添加sort函数和输出为止。我很沮丧 有任何想法吗?任何其他提示也将不胜感激。
#include <algorithm>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <fstream>
using namespace std;
enum sortVar { NAME, SOCSEC };
class record {
public:
friend bool operator<(record& rhs, record& name);
friend ostream& operator<<(ostream& out, record& toWrite);
friend istream& operator>>(istream& in, record& toRead);
bool t_sort;
private:
string firstName, lastName, ssn;
};
bool operator<(record& rhs, record& next)
{
if (rhs.t_sort = false) {
if (rhs.lastName == next.lastName)
return rhs.firstName < next.firstName;
else
return rhs.lastName < next.lastName;
}
else if (rhs.t_sort = …Run Code Online (Sandbox Code Playgroud) 下面的程序,一个简单的向量排序,在第二次排序调用时崩溃t> = 17.即使对于t == 100,第一次排序也会成功.挣扎了很长一段时间,但我无法弄清楚出了什么问题.有人可以帮帮我吗?
我已经在MacBook Air和Linux机器上试过了,令人惊讶的是,我看到了同样的结果.
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct tc
{
unsigned int n;
};
bool sort_by_n( tc a, tc b )
{
return a.n <= b.n;
}
vector<tc> tcv(100);
vector<int> tv(100);
int main()
{
unsigned int t;
cin >> t;
for ( unsigned int i = 0 ; i < t ; i++ )
{
cin >> tcv[i].n;
tv[i] = tcv[i].n;
}
sort( tv.begin(), tv.begin()+t); // ## This one works even for t == …Run Code Online (Sandbox Code Playgroud) 我有一段代码如下:
#include<algorithm>
#include<vector>
std::vector<std::string> vect;
std::vector<std::string> * vectP;
vect.push_back("ele0");
vect.push_back("ele1");
void func(){
if(std::find(*vectP.begin(),*vecP.end(),"ele0")!=*vectP.end())
//'begin' and 'end' have not been declared
}
Run Code Online (Sandbox Code Playgroud)
为什么begin和end当他们正在与指针使用未声明?*vectP应该被视为是vect因为它被解除引用,或者我误解了什么?我该如何纠正这个?
我有一个vector<int*>,我想把所有尖头的元素放入一个vector<int>.所有未指向的元素都设置为nullptr.
我在考虑做这样的事情:
vector<int> copy_valid_elements(vector<int*> piv)
{
vector<int> result;
result.reserve(piv.size());
auto end_it = std::remove_if(piv.begin(), piv.end(), [](int* p) { return !p; });
std::transform(piv.begin(), end_it, back_inserter(result), [](int* p) { return *p; });
return result;
}
Run Code Online (Sandbox Code Playgroud)
但这需要不必要地移动阵列中的元素.我可以做一个for循环,但我希望有一个算法是std::copy_if和之间的交叉std::transform.有这样的野兽吗?
Planters我有一个包含 s 向量的类向量Plant。我的目标是返回一个植物向量,其中包含来自 Planter 1 的植物,然后是来自 Planter 2 的植物,依此类推。\n示例:planter{{1,2,3,4}, {2,3,4,5}}应该导致{1,2,3,4,2,3,4,5}. 请注意,数字代表植物对象。我试图用join_view它来压平它,但我收到了错误
error: class template argument deduction failed:\n 18 | plants = std::ranges::join_view(planterView);\n | ^\n/home/parallels/CMPT373/se-basic-cpp-template/lib/solutions/task05.cpp:18:52: error: no matching function for call to \xe2\x80\x98join_view(std::ranges::ref_view<std::vector<ex4::Planter> >&)\xe2\x80\x99\nRun Code Online (Sandbox Code Playgroud)\n我已经尝试过以下操作:
\nfor (auto it : planters){\n plants.insert(plants.end(), it.getPlants().begin(), it.getPlants().end());\n}\nRun Code Online (Sandbox Code Playgroud)\n这是可行的,但是我只允许使用一个循环(不包括 STL 函数调用内的循环)并且只能分配内存一次。上述方法多次分配内存。我该如何处理这个问题?
\n我的代码:
\nstd::vector<Plant> task05(std::vector<Planter> planters){\n std::vector<Plant> plants;\n auto planterView = std::views::all(planters);\n std::views::transform(planterView, [](Planter planter){ return planter.getPlants();});\n plants = ranges::views::all(std::ranges::join_view(planterView));\n return plants;\n}\n …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建自己的迭代器,并且我已经使用std :: generate算法按预期工作了.但是,当我尝试std :: find的std :: max_element时,我得到一些神秘的错误.
这是我的迭代器的接口:
template <typename GridT,
typename GridPtr,
typename GridRef,
template <typename> class ShapeT>
class GridIterator
{
public:
typedef GridIterator<GridT, GridPtr, GridRef, ShapeT> Iterator;
// Iterator traits - typedefs and types required to be STL compliant
typedef std::ptrdiff_t difference_type;
typedef typename GridT::Element value_type;
typedef typename GridT::Element* pointer;
typedef typename GridT::Element& reference;
typedef size_t size_type;
std::forward_iterator_tag iterator_category;
GridIterator(GridT& grid,
ShapeT<typename GridT::Resolution> shape,
Index iterStartIndex);
~GridIterator();
Iterator& operator++();
Iterator operator++(int);
typename GridT::Element& operator*();
typename GridT::Element* operator->();
bool …Run Code Online (Sandbox Code Playgroud) 有一个旧的遗留代码填充istream中的向量,向量中的对象接受带有原始数据的字符串.
typedef std::vector<MyClass*> my_array;
std::istream& operator >> (std::istream& s, my_array& arr) {
if (s) {
std::istream_iterator<std::string> i_iter = s;
for(++i_iter; !s.eof(); arr.push_back(new MyClass(*i_iter++)));
}
return s;
}
Run Code Online (Sandbox Code Playgroud)
MyClass只有ctor的样子:
MyClass(const std::string& data);
Run Code Online (Sandbox Code Playgroud)
你看到一些方法来避免写入操作符>>或任何其他函数,并使用一些(?)标准算法来填充刚刚构造的对象的容器?可能用emplace contructing替换容器中值的指针.
顺便说一句,这个用VC10编译的代码不能正常工作,当我踩到for时看起来像无限循环.然而istream(实际上这是ifstream那里)是一个小文件~200行文本
c++ ×10
stl-algorithm ×10
stl ×5
iterator ×2
sorting ×2
algorithm ×1
arrays ×1
c++20 ×1
containers ×1
linux ×1
pointers ×1
std-ranges ×1
vector ×1