嘿伙计们我有一个小问题.
有人可以告诉我如何将这个字符串分成json格式的数据吗?
one:apple;two:orange;three:bananna;four:pears
Run Code Online (Sandbox Code Playgroud)
看起来像这样
{
one: apple,
two: orange,
three: bananna,
four: pears
}
Run Code Online (Sandbox Code Playgroud) 我试图为一些C++数据结构编写一个C包装器.现在我有以下内容foo.cpp
typedef std::map<unsigned int, void *> _Map;
extern "C"{
void* map_create()
{
return reinterpret_cast<void*> (new _Map);
}
void map_put(void *map, unsigned int k, void *v)
{
Map *m = reinterpret_cast<_Map *> (map);
m->insert(std::pair<unsigned int, void *>(k, v));
}
}
Run Code Online (Sandbox Code Playgroud)
在foo.h我有
#ifdef __cplusplus
#define EXTERNC extern "C"
#else
#define EXTERNC
#endif
typedef void *Map;
EXTERNC void *map_create();
EXTERNC void map_put(void* map, unsigned int k, int v);
Run Code Online (Sandbox Code Playgroud)
我包括foo.h,我很高兴去.
现在,我想迭代一张地图并注意到C++通过迭代器来做到这一点.我没有使用C++的经验,也不知道如何实现迭代器.
我可以std::map使用C包装器迭代吗?这些函数定义将如何呈现?我将如何在我的C代码中的for循环中使用它们?
我是一个c ++菜鸟,我无法想出这个.我正在尝试使用模块参数作为键在地图中搜索值,如下所示.我得到的第一个错误是下面指示的行上的操作符等于不匹配,第二个错误是下面显示的行上的"期望的主要表达式"''令牌".
float Student::getMark(const string &module) const throw (NoMarkException){ //TODO
map<string, float>::iterator p;
p = marks.find(module); //no match for operator=
if(p != marks.end())
return p->second;
else
throw (NoMarkException); //expected primary-expression before ')' token
return 0.0;
}
Run Code Online (Sandbox Code Playgroud)
任何帮助将非常感激!
我有这门课:
class foo{
public:
const std::vector<int> get_v() const{
return v_;
}
private:
std::vector<int> v_
};
Run Code Online (Sandbox Code Playgroud)
我能这样用吗?
int main(){
foo f;
some_non_inplace_std_function(f.get_v().cbegin(),f.get_v().cend());
}
Run Code Online (Sandbox Code Playgroud)
第一个f.get_v()指向第二个相同的向量f.get_v()吗?
我正在尝试创建pub fn sing(start: i32, end: i32) -> String它返回一个串联的字符串,pub fn verse(num: i32) -> String重复调用每个数字之间start和end.
我用google搜索答案,看起来Rust String连接回答了我的问题,如果我甚至在操场上编写我的代码就行了,但是:
我的代码:
pub fn verse(num: i32) -> String {
match num {
0 => "No more bottles of beer on the wall, no more bottles of beer.\nGo to the store and buy some more, 99 bottles of beer on the wall.\n".to_string(),
1 => "1 bottle of beer on the wall, 1 bottle of beer.\nTake …Run Code Online (Sandbox Code Playgroud) 我正在编写一个实现迭代器的内部类,我正在尝试为我的一个方法返回一个整数,但它不会让我.我知道这是一个非常基本的问题,但我是java的新手,所以我很抱歉听起来很简单.
public class pIterator<Integer> implements Iterator<Integer>{
private int currentEx = 1;
private int base = 4;
//error says can't convert int to Integer here specifically
private Integer changeToInteger = base * currentEx++;
@Override
public boolean hasNext() {
return currentEx <= max;
}
//the problem then occurs here when I try to return an Integer
@Override
public Integer next() throws NoSuchElementException {
if (currentEx > max) throw new NoSuchElementException();
return changeToInteger;
}
}
Run Code Online (Sandbox Code Playgroud)
currentEx和base必须是int(由指令定义)所以我应该只更改返回类型还是可以转换为整数?
我正在使用ArrayList的iterator:
List<String> al = new ArrayList<>();
// ----- Logic for adding elements-----
Iterator it = al.iterator();
// logic to retrieve elements----
Run Code Online (Sandbox Code Playgroud)
然后它尝试使用ListIterator,就像这样.
ListIterator li = al.listIterator();
while(li.hasNext()) {
System.out.print(li.next()+" ");
}
Run Code Online (Sandbox Code Playgroud)
有效 ...
我试着这个用于反向检索
ListIterator li = al.listIterator();
while(li.hasPrevious()) {
System.out.print(li.previous()+" ");
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
以下代码正常运行.
ListIterator<String> li = al.listIterator(al.size());
while(li.hasPrevious()) {
System.out.println(li.previous()+" ");
}
Run Code Online (Sandbox Code Playgroud)
我想知道有一些泛型的概念,但不清楚它.请清除Iterator和ListIterator的概念.为什么ListIterator的一个语句正在运行另一个而不是?
我有两种方法来遍历包含键(String)和值(Arraylist)的哈希映射,并将Arraylists中的所有值添加到单个Arraylist中.
方法一不起作用,所以我创建了方法二,修复了问题,但我不确定为什么方法一不起作用.有人可以解释为什么方法二有效,方法一无效?
方法1
public ArrayList<Person> getPeopleList()
{
Iterator<ArrayList<Person>> iter = people.values().iterator();
ArrayList<Person> allPersons = new ArrayList<>();
while (iter.hasNext())
{
for (int i = 0; i < iter.next().size(); i++)
{
allPersons.add(iter.next().get(i));
}
}
return allPersons;
}
Run Code Online (Sandbox Code Playgroud)
方法2
public ArrayList<Person> getPeopleList()
{
Iterator<ArrayList<Person>> iter = people.values().iterator();
ArrayList<Person> allPersons = new ArrayList<>();
ArrayList<Person> persons;
while (iter.hasNext())
{
persons = iter.next();
for (Person p : persons)
{
allPersons.add(p);
}
}
return allPersons;
}
Run Code Online (Sandbox Code Playgroud) 给出一个std :: list
std::list< int > myList
Run Code Online (Sandbox Code Playgroud)
以及该列表中元素的引用(或指针)
int& myElement | int* pElement
Run Code Online (Sandbox Code Playgroud)
所以,基本上我知道那个元素的地址
如何有效地获得std::list<int>::iterator该元素?
一个缓慢但有效的例子是
const_iterator it
for( it = myList.begin(); it != &myElement; ++it)
{
// do nothing, for loop terminates if "it" points to "myElem"
}
Run Code Online (Sandbox Code Playgroud)
有更快的方法吗?喜欢
const_iterator it = magicToIteratorConverter( myList, myElem )
Run Code Online (Sandbox Code Playgroud)
对于矢量,您可以执行以下操作:
const int* pStart = &myVector[0] // address of first element
const int* pElement = &myElem; // address of my element
const idx = static_cast< int >( pElement- pStart ); …Run Code Online (Sandbox Code Playgroud) 这个:
class Loan
def initialize(amount, interest)
@amount = amount
@interest = interest
end
end
loan1 = Loan.new(100, 0.1)
Loan.each do |amount, interest|
debt = debt + amount + (amount*interest)
end
Run Code Online (Sandbox Code Playgroud)
将无法工作,因为它试图迭代一个类而不是数组或散列.是否需要迭代一个类的所有实例?