C++ 类字符串和嵌套向量C++;
调试以下代码时遇到困难。然而,输出有点奇怪C++ 类字符串和嵌套向量C++;,c++,string,sorting,vector,C++,String,Sorting,Vector,调试以下代码时遇到困难。然而,输出有点奇怪 Compilation successful Running tests..... empty vector passed small sorted failed input: abc def ghi jkl mno pqrs tuv wxyz expected output: abc def ghi jkl mno pqrs tuv wxyz actual output: abc def�� ghi jkl mno pqrs tuv wxyz�
Compilation successful
Running tests.....
empty vector passed
small sorted failed
input:
abc def ghi jkl mno pqrs tuv wxyz
expected output:
abc def ghi jkl mno pqrs tuv wxyz
actual output:
abc def�� ghi jkl mno pqrs tuv wxyz�
--------------------------------------------------------------
small decreasing failed
input:
wxyz tuv pqrs mno jkl ghi def abc
expected output:
abc def ghi jkl mno pqrs tuv wxyz
actual output:
abc�� def ghi jkl mno pqrs tuv wxyz
--------------------------------------------------------------
small randomised failed
input:
pqrs wxyz jkl tuv abc mno def ghi
expected output:
abc def ghi jkl mno pqrs tuv wxyz
actual output:
abc def ghi�� jkl mno pqrs tuv wxyz
--------------------------------------------------------------
large failed
input:
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
occaecat cupidatat non proident, sunt in culpa qui officia deserunt
mollit anim id est laborum.
expected output:
Duis Excepteur Lorem Ut ad adipiscing aliqua. aliquip amet, anim aute
cillum commodo consectetur consequat. culpa cupidatat deserunt do dolor
dolor dolore dolore ea eiusmod elit, enim esse est et eu ex
exercitation fugiat id in in in incididunt ipsum irure labore laboris
laborum. magna minim mollit nisi non nostrud nulla occaecat officia
pariatur. proident, qui quis reprehenderit sed sint sit sunt tempor
ullamco ut ut velit veniam, voluptate
actual output:
Duis� Excepteur���� Lorem Ut��� ad��� adipiscing aliqua. aliquip amet,
anim� aute� cillum commodo consectetur�� consequat.��� culpa
cupidatat���� deserunt����� do��� dolor dolore dolore dolor ea���
eiusmod elit, enim� esse� est�� et��� eu��� ex��� exercitation� fugiat
id��� in��� in��� in��� incididunt��� ipsum irure labore laboris
laborum.����� magna minim mollit nisi� non�� nostrud nulla
occaecat����� officia pariatur.���� proident,���� qui�� quis�
reprehenderit sed�� sint� sit sunt� tempor ullamco ut��� ut velit
veniam, voluptate����
--------------------------------------------------------------
我现在不明白为什么程序会产生如此奇怪的输出。
如下所示,我包括编写的代码
#include <algorithm>
#include <iostream>
template <class T>
class Vector
{
public:
// member types
using value_type = T;
using iterator = T*;
using const_iterator = const T*;
using reference = T&;
using const_reference = const T&;
// constructors
Vector () : first {nullptr}, last {nullptr}, limit {nullptr} {}
Vector (std::size_t size) : Vector (size, size) {}
Vector (const Vector& vector) : Vector (vector.last - vector.first)
{
std::copy (vector.first, vector.last, first);
}
Vector (Vector&& vector) : Vector ()
{
swap (vector);
}
// deconstructor
~Vector ()
{
delete [] first;
}
// assignment
Vector& operator = (const Vector& vector)
{
Vector copy {vector};
swap (copy);
return *this;
}
Vector& operator = (Vector&& vector)
{
swap (vector);
return *this;
}
// iterators
iterator begin ()
{
return first;
}
iterator end ()
{
return last;
}
const_iterator begin () const
{
return first;
}
const_iterator end () const
{
return last;
}
std::size_t size () const
{
return last - first;
}
// element access
reference operator [] (std::size_t index)
{
return first[index];
}
const_reference operator [] (std::size_t index) const
{
return first[index];
}
// modifiers
void swap (Vector& vector)
{
std::swap (first, vector.first);
std::swap (last, vector.last);
std::swap (limit, vector.limit);
}
void push_back (const value_type& value)
{
if (last == limit)
{
std::size_t size = last - first;
Vector vector {size, size * 2 + 1};
std::move (first, last, vector.first);
swap (vector);
}
*last = value;
++last;
}
void pop_back (const value_type& value)
{
std::size_t size = last - first;
std::size_t cap = limit - last;
if (cap > 2 && size <= cap / 4)
{
Vector vector {size, size * 2 + 1};
std::move (first, last, vector.first);
swap (vector);
}
}
void clear ()
{
last = first;
}
private:
Vector (std::size_t size, std::size_t capacity) : first {new value_type[capacity]}, last {first + size}, limit {first + capacity} {}
iterator first;
iterator last;
iterator limit;
};
template<typename T>
Vector<T> read ()
{
Vector<T> vector;
for (T value; std::cin >> value;) vector.push_back (value);
return vector;
}
template<typename T>
void sort (T* begin, T* end)
{
std::size_t size = end - begin;
for (std::size_t index = 0; index != size; ++index)
{
auto minimum = index;
for (auto comparand = minimum; comparand != size; ++comparand)
if (*(begin + comparand) < *(begin + minimum))
minimum = comparand;
if (minimum != index)
std::swap (*(begin + minimum), *(begin + index));
}
}
template<typename T>
void write (T* begin, T* end)
{
std::size_t size = end - begin;
for (std::size_t index = 0; index != size; ++index)
std::cout << *(begin + index) << ' ';
}
class String
{
public:
// constructors
String () {}
// iterators
char* begin ()
{
return string.begin();
}
char* end ()
{
return string.end();
}
const char* begin () const
{
return string.begin();
}
const char* end () const
{
return string.end();
}
// modifiers
void clear ()
{
string.clear();
}
String& operator += (char character)
{
string.push_back(character);
}
private:
Vector<char> string;
std::size_t size;
};
bool operator < (const String& a, const String& b)
{
return std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end());
}
std::ostream& operator << (std::ostream& os, const String& string)
{
os << string.begin();
return os;
}
std::istream& operator >> (std::istream& is, String& string)
{
string.clear ();
while (is.good () && std::isspace (is.peek ())) is.ignore ();
while (is.good () && std::isgraph (is.peek ())) string += is.get ();
if (string.begin () == string.end ()) is.setstate (is.failbit);
return is;
}
int main ()
{
auto vector = read<String> ();
sort (vector.begin (), vector.end ());
write (vector.begin (), vector.end ());
}
#包括
#包括
模板
类向量
{
公众:
//成员类型
使用值_type=T;
使用迭代器=T*;
使用常量迭代器=常量T*;
使用reference=T&;
使用const_reference=const T&;
//建设者
向量():第一个{nullptr},最后一个{nullptr},极限{nullptr}{
向量(std::size\u t size):向量(size,size){}
向量(常量向量和向量):向量(Vector.last-Vector.first)
{
std::copy(vector.first,vector.last,first);
}
向量(向量和向量):向量()
{
交换(向量);
}
//解构器
~Vector()
{
首先删除[];
}
//分配
向量和运算符=(常量向量和向量)
{
向量拷贝{Vector};
互换(复印件);
归还*这个;
}
向量和运算符=(向量和向量)
{
交换(向量);
归还*这个;
}
//迭代器
迭代器begin()
{
先返回;
}
迭代器结束()
{
最后返回;
}
常量迭代器begin()常量
{
先返回;
}
常量迭代器end()常量
{
最后返回;
}
std::size\u t size()常量
{
返回最后优先;
}
//元素访问
参考运算符[](标准::大小索引)
{
先返回[索引];
}
常量参考运算符[](标准::大小索引)常量
{
先返回[索引];
}
//修饰语
无效交换(向量和向量)
{
std::swap(第一,vector.first);
std::swap(最后一个,vector.last);
标准::交换(限制,向量限制);
}
无效推回(常量值类型和值)
{
如果(最后==限制)
{
std::size\u t size=last-first;
向量{size,size*2+1};
std::move(first,last,vector.first);
交换(向量);
}
*last=值;
++最后;
}
无效弹出窗口返回(常量值类型和值)
{
std::size\u t size=last-first;
标准::尺寸\u t上限=极限-最后;
如果(cap>2&&size>value;)vector.push_back(值);
返回向量;
}
模板
无效排序(T*开始,T*结束)
{
std::size\u t size=结束-开始;
对于(std::size\u t index=0;index!=size;++index)
{
自动最小=索引;
用于(自动比较=最小值;比较!=大小;++比较)
如果(*(开始+比较)<*(开始+最小值))
最小值=可比值;
如果(最小值!=索引)
标准::交换(*(开始+最小值),*(开始+索引));
}
}
模板
无效写入(T*开始,T*结束)
{
std::size\u t size=结束-开始;
对于(std::size\u t index=0;index!=size;++index)
std::cout您的问题中没有代码重现您观察到的问题
我发现了几个问题:
复制构造函数分配内存,然后复制元素。如果复制抛出析构函数,则析构函数永远不会运行,并且会泄漏内存
非公共分配构造函数使用new
调用元素的默认构造函数。如果下一步要做的事情是覆盖复制构造函数中的元素,那么这将是徒劳的
不确定这是否是观察到的输出的原因,但在调用方法时它肯定会导致未定义的行为。您的Vector::end()
是:
const_iterator end () const {
last;
}
此方法不返回任何内容
请注意,一个好的编译器应该警告您该语句无效。我关闭该问题是因为我找到了答案。感谢所有帮助我的人。检查您的end()
method…你这是什么意思?@user463035818看到我的答案了吗below@ArisMartinAccola使用unique\u ptr
作为first
或您自己的RAII包装器。您能给我写下您所说的代码片段吗,因为我不太懂。我对编程非常陌生。非常感谢。@arismartinacolastd::unique\u ptr-first;
。到目前为止,我可以理解你的意思,但我不知道我需要在哪里碰碰运气。你是指它而不是首先使用迭代器还是其他地方?@Aristinancola将Vector::first
的类型从iterator
更改为std::unique\u ptr
。哦,这是一个多么严重的错误。所以我将它改为return last;
但这是一个错误无法解决问题。@正如其他人提到的,您没有提供足够的信息来重现问题(同时代码有点太多)。请使用调试器缩小问题范围,并提供一个示例。我已经运行了调试器,但找不到故障。因此,我向她寻求帮助。我在描述中已经提到,我认为问题出在扩展函数中。我附上了孔代码,因为其他部分也可能是错误。
std::ostream& operator << (std::ostream& os, const String& string)
{
for(auto x : string)
os << x;
return os;
}
const_iterator end () const {
last;
}