此方法接受一组字符串,然后删除该组的偶数长度的所有字符串.问题是我知道集合不计入元素所以我必须使用迭代器,但是,如何从集合中删除特定的"元素"?
private static void removeEvenLength(Set<String> thing) {
Iterator<String> stuff = thing.iterator();
while (stuff.hasNext()) {
String temp = stuff.next();
if (temp.length() %2 == 0) {
temp.remove(); // What do I do here?
}
}
}
Run Code Online (Sandbox Code Playgroud) 我想写一些类似于以下代码的东西
class c{
public:
//...
big_structure* find(int e){
auto it = std::lower_bound(v1.begin(), v1.end(), e);
return v2[it - v1.begin()];
}
private:
std::vector<int> v1; //v1 is sorted;
std::vector<big_structure*> v2; //v2.size() = v1.size() + 1
}
Run Code Online (Sandbox Code Playgroud)
这是合法的,而且当e不在v1中时它会返回v2 [v1.size()]吗?
如果可能的话我不想特殊情况它== v1.end().
我得到一个C2440('初始化':无法从'std :: _ Vb_reference <_Alloc>'转换为'bool&'),IntelliSense转换为标题中的错误.
我得到了这个错误所说的内容,而不是为什么会这么说.下面的代码产生此错误:
std::vector<const UINT>::iterator oIter;
oIter = std::find(vecuClassID.begin(), vecuClassID.end(), uClassID);
const UINT uDistance = std::distance(vecuClassID.begin(), oIter);
bool& refbStaticSectionInitialized = *(vecbStaticSectionInitialized.begin() + uDistance);
Run Code Online (Sandbox Code Playgroud)
错误似乎发生在最后一行 - 在Visual Studio中,取消引用运算符以红色下划线.这很令人困惑,因为我的代码与CRITICAL_SECTION完全相同,并且不会产生错误:
std::vector<const UINT>::iterator oIter;
oIter = std::find(vecuClassID.begin(), vecuClassID.end(), uClassID);
const UINT uDistance = std::distance(vecuClassID.begin(), oIter);
CRITICAL_SECTION& refhStaticSection = *(vechStaticSection.begin() + uDistance);
Run Code Online (Sandbox Code Playgroud)
它与bool是一个原始的东西有关吗?
我在C++ 03环境中工作,并将一个函数应用于地图的每个键是很多代码:
const std::map<X,Y>::const_iterator end = m_map.end();
for (std::map<X,Y>::const_iterator element = m_map.begin(); element != end; ++element)
{
func( element->first );
}
Run Code Online (Sandbox Code Playgroud)
如果key_iterator存在,相同的代码可以利用std::for_each:
std::for_each( m_map.key_begin(), m_map.key_end(), &func );
Run Code Online (Sandbox Code Playgroud)
那为什么不提供呢?有没有办法让第一种模式适应第二种模式?
因此,根据我发现的其他示例,我会相信这将是迭代m_vect的正确代码:
for(vector<T>::iterator it = m_vect.begin(); it != m_vect.end(); ++it)
Run Code Online (Sandbox Code Playgroud)
但是,在尝试编译时,我在该行上收到以下错误:
heap.h:167:6: error: need ‘typename’ before ‘std::vector<T>::iterator’ because ‘std::vector<T>’ is a dependent scope
Run Code Online (Sandbox Code Playgroud)
就像我说的那样,我从另一段代码中复制并修改了这一行,所以我真的不确定我做的是非对错.任何见解?
为了澄清,这是一个模板函数,我已经声明了'模板'.m_vect是vector类型.Aaaand我不知道如何显示小于和大于正确...
我试图通过向量反向迭代.我使用typedef"制作"了一个迭代器:
typedef std::vector<Object*>::iterator Cursor;
Run Code Online (Sandbox Code Playgroud)
我的问题是,当它到达向量的开头时,这个函数似乎崩溃了.我有以下代码:
void InsertFunc(Cursor& it, Object& o) {
vec_.insert(it, o);
--it;
for (; it >= vec_.begin(); --it) {
if ((*it)->type() == Object::SomeType) {
do_something
} else {
do_something_else
}
}
std::cout << "Insertion succes!" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
我已经测试过了,我肯定知道函数到达向量的开头,然后程序才会终止并且消息"Insertion succes!" 永远不会打印.知道为什么吗?
我无法找到解决问题的方法.让我们假设我们有这样的地图的n个元素:
std::map<string, string> tmpMap;
Run Code Online (Sandbox Code Playgroud)
我们也有这个地图的迭代器:
std::map<string, string>::iterator itTmpMap = tmpMap.begin();
Run Code Online (Sandbox Code Playgroud)
我现在如何将迭代器更改为例如第四对?Compilator不允许我只是向迭代器添加一个整数.
Compilator error:
int a = 4;
itTmpMap = itTmpMap + a;
Run Code Online (Sandbox Code Playgroud) 当我在ArrayList上获取迭代器并在其上使用方法next()时,它是仅返回下一个元素还是从迭代器返回并删除元素.喜欢
Iterator i = list.iterator();
while(i.hasNext())
System.out.print(i.next()+" ");
Run Code Online (Sandbox Code Playgroud) 谢谢阅读.我是编程新手,我正在尝试编写一个迭代所有i值的函数.例如,它可能是function3(),然后迭代到随机函数12().这是我到目前为止:
int main()
{
trigFunction();
system("pause");
return 0;
}
void trigFunction()
{
int i;
cout << "Welcome to Haris's Unit Circle Fun House!\n";
srand(time(NULL));
i = rand()%15;
for (int x = 1; x < 15; x++)
{
functioni(); //This is obviously wrong. Is there way to do this correctly?
i = rand() % 15;
}
}
Run Code Online (Sandbox Code Playgroud)
functioni()我在下面定义了文本,例如:
void function3()
{
cout << "What is 5pi/3 in degrees?\n";
cin >> answer;
if (answer == 300)
{
cout << "Correct answer!\n";
} …Run Code Online (Sandbox Code Playgroud) 我目前正在开发一个关于一个应用程序的项目,它能够从绘制的图像中创建一个可玩的游戏关卡(可以在这里找到:Github).为此,我使用openCV进行图像处理.
我的问题是一个函数,它应该将检测到的线条(水平面上的"墙壁")绘制到图像上.
void LineFinder::drawDetectedLines( cv::Scalar color)
{
for (auto it = Play::getInstance()->getFinder()->getLines().begin(); it != Play::getInstance()->getFinder()->getLines().end(); ++it)
{
// The lines are stored in an std::vector<cv::Vec4i>,
// so basically in a vector which contains vectors with 4 elements each
cv::Point pt1((*it)[0], (*it)[1]);
cv::Point pt2((*it)[2], (*it)[3]);
// draws a line from pt1 to pt2
cv::line(Play::getInstance()->getFinder()->getImage(), pt1, pt2, color);
++it;
}
}
Run Code Online (Sandbox Code Playgroud)
当这个函数执行时,大多数时候我都会遇到分段错误,但有时它会起作用,结果就像我预期的那样.我知道向量包含元素.
那么你能想到的这种行为有什么可能的原因吗?
编辑:有趣的是,当使用此函数的函数已成功执行一次时,我可以反复执行它,并且不会发生错误.
编辑2:我仍然不知道为什么我用迭代器得到分段错误,但没有它们它以某种方式工作:
void LineFinder::drawDetectedLines( cv::Scalar color)
{
for (int i = 0; i < Play::getInstance()->getFinder()->getLines().size(); ++i)
{ …Run Code Online (Sandbox Code Playgroud)