I have the following test code:
class Person{};
shared_ptr<Person> sp1;
shared_ptr<Person> sp2;
shared_ptr<Person> sp3;
vector<shared_ptr<Person>> members = {sp1,sp2,sp3};
map<string, shared_ptr<Person>> mymap;
How can I set the vector members to the second element of mymap? I tried with for and copy .
map is an associative container, so you need to specify the key in string type to associate it to the shared_ptr<Person>. I don't know what you need, but the following loop should work:
for (size_t i = 0; i < members.size(); ++i) {
const std::string key = "sp" + std::to_string(i+1);
mymap[key] = members[i];
}
I have List and I want to convert to Map<Integer,String> using streams in java8.
Say for example :
List<Integer> li = Arrays.asList(1,2,3);
Then want to convert to Map<Integer,String> like
Map({1,"1"},{2,"2"},{3,"3"})
You can try below stuff and should work fine(Tested).
List<Integer> li = Arrays.asList(1,2,3);
Map<Integer, String> result =
li.stream().collect(Collectors.toMap(i -> i, i -> i.toString()));
If you want map, there should be Key=Value pair, I assume you [{1,"1"}, {2,"2"}, {3,"3"}] want like this,
List<String> collect = li.stream()
.map(a -> "{"+a + ",\"" + a +"\"}")
.collect(Collectors.toList());
Im new to Java and JavaFX and Im trying to check it there is a duplicate in a Tableview, and if that is the case I would like to replace it with a new set of data.
So in essence I'm trying to iterate through the data in my TableView and compare it to something. To be more exact I'd like to compre a value of the String on the first column to a new String. I've done some research and I've found that the most common kind of solution for Filtering Data is using a FilteredList but this doesn't return my original set of items.
my current Code looks like this:
#FXML private TableView<STable> TableV;
public void Replace(String s){
ObservableList<STable> getCurrentData;
for(int i = 0; i < getCurrentData.size(); i++){
// Here is where I get Stuck I've tried:
//TableV.getSelectionModel().getSelectedItem().getCajas();
//getCurrentData.get(i)
}
}
Note: The STable is a class that has all the setters and getters for each of the columns, I've also got the CellFactory set up.
Any guidance on how to do this would be great!
Basically you just have to iterate through your data items, and compare the value representing the content of column 1, to your new string. If both values are equal, you update the value in your dataModel:
(I replaced STable with YourData, because I find the name for a dataModel a little confusing)
for (YourData data : tableView.getItems()) {
if (data.getColumOne().equals(textToCompare)) {
data.setColumnOne("newText");
}
}
Or if you want to replace the row:
for (int idx = 0; idx < tableView.getItems().size(); idx++) {
YourData data = tableView.getItems().get(idx);
if (data.getColumnOne().equals(textToCompare)) {
tableView.getItems().set(idx, someOtherData);
return;
}
}
I want to find similar strings in an array/dictionary and rename them by adding a postfix to the string. For example suppose my array/dictionary contains strings like this:
1.Work
2.Work
3.Work
4.Home
5.Home
6.Mobile
7.Mobile
etc. there could be any similar strings. Now I want to rename them like this:
1.Work(1)
2.Work(2)
3.Work(3)
4.Home(1)
5.Home(2)
6.Mobile(1)
7.Mobile(2)
Well, i don't know about a dictionary, but for an array you could do something like this.
NSMutableArray *myArr = [fill the array with w/e];
int home;
int mobile;
int work;
for(int x = 0; x < [myArr count]; x++){
NSString *r = [myArr objectAtIndex:(NSUInteger)x];
if([r isEqualToString:#"work"]){
work++;
[myArr replaceObjectAtIndex:x withObject:[NSString strinWithFormat:#"%#(%i)", r, work]];
}
else if([r isEqualToString:#"home"]){
home++;
[myArr replaceObjectAtIndex:x withObject:[NSString strinWithFormat:#"%#(%i)", r, home]];
}
else if([r isEqualToString:#"mobile"]){
mobile++;
[myArr replaceObjectAtIndex:x withObject:[NSString strinWithFormat:#"%#(%i)", r, mobile]];
}
}
Best of luck
I've got a JS array which is writing to a text file on the server using StreamWriter. This is the line that does it:
sw.WriteLine(Request.Form["seatsArray"]);
At the moment one line is being written out with the entire contents of the array on it. I want a new line to be written after every 5 commas. Example array:
BN,ST,A1,303,601,BN,ST,A2,303,621,BN,WC,A3,303,641,
Should output:
BN,ST,A1,303,601,
BN,ST,A2,303,621,
BN,WC,A3,303,641,
I know I could use a string replace but I only know how to make this output a new line after every comma, and not after a specified amount of commas.
How can I get this to happen?
Thanks!
Well, here's the simplest answer I can think of:
string[] bits = Request.Form["seatsArray"].Split(',');
for (int i = 0; i < bits.Length; i++)
{
sw.Write(bits[i]);
sw.Write(",");
if (i % 5 == 4)
{
sw.WriteLine();
}
}
It's not terribly elegant, but it'll get the job done, I believe.
You may want this afterwards to finish off the current line, if necessary:
if (bits[i].Length % 5 != 0)
{
sw.WriteLine();
}
I'm sure there are cleverer ways... but this is simple.
One question: are the values always three characters long? Because if so, you're basically just breaking the string up every 20 characters...
Something like:
var input = "BN,ST,A1,303,601,BN,ST,A2,303,621,BN,WC,A3,303,641,";
var splitted = input.Split(',');
var cols = 5;
var rows = splitted.Length / cols;
var arr = new string[rows, cols];
for (int row = 0; row < rows; row++)
for (int col = 0; col < cols; col++)
arr[row, col] = splitted[row * cols + col];
I will try find a more elegant solution. Properly with some functional-style over it.
Update: Just find out it is not actually what you needs. With this you get a 2D array with 3 rows and 5 columns.
This however will give you 3 lines. They do not have a ending ','. Do you want that? Do you always want to print it out? Or do you want to have access to the different lines?:
var splitted = input.Split(new [] { ','}, StringSplitOptions.RemoveEmptyEntries);
var lines = from item in splitted.Select((part, i) => new { part, i })
group item by item.i / 5 into g
select string.Join(",", g.Select(a => a.part));
Or by this rather large code. But I have often needed a "Chunk" method so it may be reusable. I do not know whether there is a build-in "Chunk" method - couldn't find it.
public static class LinqExtensions
{
public static IEnumerable<IList<T>> Chunks<T>(this IEnumerable<T> xs, int size)
{
int i = 0;
var curr = new List<T>();
foreach (var x in xs)
{
curr.Add(x);
if (++i % size == 0)
{
yield return curr;
curr = new List<T>();
}
}
}
}
Usage:
var lines = input.Split(',').Chunks(5).Select(list => string.Join(",", list));